annotate Lib/test/test_unicode.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 995cddd15313
children 102df748572d 0df295d590a8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """ Test script for the Unicode implementation.
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 Written by Marc-Andre Lemburg (mal@lemburg.com).
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
15957
c1a8deb15f0e This patch removes all uses of "assert" in the regression test suite
Marc-André Lemburg <mal@egenix.com>
parents: 15910
diff changeset
7 """#"
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
8 import codecs
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
9 import struct
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
10 import sys
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
11 import unittest
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
12 import warnings
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46307
diff changeset
13 from test import support, string_tests
67791
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
14 import _string
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
15
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
16 # decorator to skip tests on narrow builds
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
17 requires_wide_build = unittest.skipIf(sys.maxunicode == 65535,
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
18 'requires wide build')
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
19
35938
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
20 # Error handling (bad decoder return)
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
21 def search_function(encoding):
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
22 def decode1(input, errors="strict"):
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
23 return 42 # not a tuple
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
24 def encode1(input, errors="strict"):
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
25 return 42 # not a tuple
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
26 def encode2(input, errors="strict"):
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
27 return (42, 42) # no unicode
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
28 def decode2(input, errors="strict"):
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
29 return (42, 42) # no unicode
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
30 if encoding=="test.unicode1":
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
31 return (encode1, decode1, None, None)
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
32 elif encoding=="test.unicode2":
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
33 return (encode2, decode2, None, None)
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
34 else:
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
35 return None
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
36 codecs.register(search_function)
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
37
59775
e619047a2f36 Clean up the warnings filter use in test_unicode.
Brett Cannon <bcannon@gmail.com>
parents: 58415
diff changeset
38 class UnicodeTest(string_tests.CommonTest,
e619047a2f36 Clean up the warnings filter use in test_unicode.
Brett Cannon <bcannon@gmail.com>
parents: 58415
diff changeset
39 string_tests.MixinStrUnicodeUserStringTest,
e619047a2f36 Clean up the warnings filter use in test_unicode.
Brett Cannon <bcannon@gmail.com>
parents: 58415
diff changeset
40 string_tests.MixinStrUnicodeTest):
e619047a2f36 Clean up the warnings filter use in test_unicode.
Brett Cannon <bcannon@gmail.com>
parents: 58415
diff changeset
41
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
42 type2test = str
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
43
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
44 def checkequalnofix(self, result, object, methodname, *args):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
45 method = getattr(object, methodname)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
46 realresult = method(*args)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
47 self.assertEqual(realresult, result)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54484
diff changeset
48 self.assertTrue(type(realresult) is type(result))
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
49
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
50 # if the original is returned make sure that
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
51 # this doesn't happen with subclasses
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
52 if realresult is object:
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
53 class usub(str):
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
54 def __repr__(self):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
55 return 'usub(%r)' % str.__repr__(self)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
56 object = usub(object)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
57 method = getattr(object, methodname)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
58 realresult = method(*args)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
59 self.assertEqual(realresult, result)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54484
diff changeset
60 self.assertTrue(object is not realresult)
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
30653
3b47d6057151 Fix for SF bug [ 817156 ] invalid \U escape gives 0=length unistr.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30059
diff changeset
62 def test_literals(self):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
63 self.assertEqual('\xff', '\u00ff')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
64 self.assertEqual('\uffff', '\U0000ffff')
42324
4bbbe0c55fa2 Merged revisions 56443-56466 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42313
diff changeset
65 self.assertRaises(SyntaxError, eval, '\'\\Ufffffffe\'')
4bbbe0c55fa2 Merged revisions 56443-56466 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42313
diff changeset
66 self.assertRaises(SyntaxError, eval, '\'\\Uffffffff\'')
4bbbe0c55fa2 Merged revisions 56443-56466 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42313
diff changeset
67 self.assertRaises(SyntaxError, eval, '\'\\U%08x\'' % 0x110000)
46307
c3dcc0c7bfd9 Added a test to make sure raw strings don't get unicode escapes
Benjamin Peterson <benjamin@python.org>
parents: 46306
diff changeset
68 # raw strings should not have unicode escapes
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65661
diff changeset
69 self.assertNotEqual(r"\u0020", " ")
30653
3b47d6057151 Fix for SF bug [ 817156 ] invalid \U escape gives 0=length unistr.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30059
diff changeset
70
47611
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
71 def test_ascii(self):
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
72 if not sys.platform.startswith('java'):
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
73 # Test basic sanity of repr()
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
74 self.assertEqual(ascii('abc'), "'abc'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
75 self.assertEqual(ascii('ab\\c'), "'ab\\\\c'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
76 self.assertEqual(ascii('ab\\'), "'ab\\\\'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
77 self.assertEqual(ascii('\\c'), "'\\\\c'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
78 self.assertEqual(ascii('\\'), "'\\\\'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
79 self.assertEqual(ascii('\n'), "'\\n'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
80 self.assertEqual(ascii('\r'), "'\\r'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
81 self.assertEqual(ascii('\t'), "'\\t'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
82 self.assertEqual(ascii('\b'), "'\\x08'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
83 self.assertEqual(ascii("'\""), """'\\'"'""")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
84 self.assertEqual(ascii("'\""), """'\\'"'""")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
85 self.assertEqual(ascii("'"), '''"'"''')
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
86 self.assertEqual(ascii('"'), """'"'""")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
87 latin1repr = (
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
88 "'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
89 "\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
90 "\\x1b\\x1c\\x1d\\x1e\\x1f !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHI"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
91 "JKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
92 "\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
93 "\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
94 "\\x9c\\x9d\\x9e\\x9f\\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\\xa8\\xa9"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
95 "\\xaa\\xab\\xac\\xad\\xae\\xaf\\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\xb6\\xb7"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
96 "\\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf\\xc0\\xc1\\xc2\\xc3\\xc4\\xc5"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
97 "\\xc6\\xc7\\xc8\\xc9\\xca\\xcb\\xcc\\xcd\\xce\\xcf\\xd0\\xd1\\xd2\\xd3"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
98 "\\xd4\\xd5\\xd6\\xd7\\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf\\xe0\\xe1"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
99 "\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\\xe8\\xe9\\xea\\xeb\\xec\\xed\\xee\\xef"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
100 "\\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
101 "\\xfe\\xff'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
102 testrepr = ascii(''.join(map(chr, range(256))))
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
103 self.assertEqual(testrepr, latin1repr)
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
104 # Test ascii works on wide unicode escapes without overflow.
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
105 self.assertEqual(ascii("\U00010000" * 39 + "\uffff" * 4096),
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
106 ascii("\U00010000" * 39 + "\uffff" * 4096))
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
107
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
108 class WrongRepr:
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
109 def __repr__(self):
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
110 return b'byte-repr'
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
111 self.assertRaises(TypeError, ascii, WrongRepr())
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
112
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
113 def test_repr(self):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
114 if not sys.platform.startswith('java'):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
115 # Test basic sanity of repr()
41620
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
116 self.assertEqual(repr('abc'), "'abc'")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
117 self.assertEqual(repr('ab\\c'), "'ab\\\\c'")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
118 self.assertEqual(repr('ab\\'), "'ab\\\\'")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
119 self.assertEqual(repr('\\c'), "'\\\\c'")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
120 self.assertEqual(repr('\\'), "'\\\\'")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
121 self.assertEqual(repr('\n'), "'\\n'")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
122 self.assertEqual(repr('\r'), "'\\r'")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
123 self.assertEqual(repr('\t'), "'\\t'")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
124 self.assertEqual(repr('\b'), "'\\x08'")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
125 self.assertEqual(repr("'\""), """'\\'"'""")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
126 self.assertEqual(repr("'\""), """'\\'"'""")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
127 self.assertEqual(repr("'"), '''"'"''')
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
128 self.assertEqual(repr('"'), """'"'""")
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
129 latin1repr = (
41620
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
130 "'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r"
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
131 "\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a"
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
132 "\\x1b\\x1c\\x1d\\x1e\\x1f !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHI"
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
133 "JKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f"
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
134 "\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d"
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
135 "\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b"
47611
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
136 "\\x9c\\x9d\\x9e\\x9f\\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
137 "\xaa\xab\xac\\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
138 "\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
139 "\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
140 "\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
141 "\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
142 "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
143 "\xfe\xff'")
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41620
diff changeset
144 testrepr = repr(''.join(map(chr, range(256))))
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
145 self.assertEqual(testrepr, latin1repr)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39544
diff changeset
146 # Test repr works on wide unicode escapes without overflow.
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
147 self.assertEqual(repr("\U00010000" * 39 + "\uffff" * 4096),
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
148 repr("\U00010000" * 39 + "\uffff" * 4096))
20017
08446a986a56 Test basic functioning of unicode repr(). (If this breaks Jython,
Guido van Rossum <guido@python.org>
parents: 19999
diff changeset
149
47611
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
150 class WrongRepr:
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
151 def __repr__(self):
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
152 return b'byte-repr'
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
153 self.assertRaises(TypeError, repr, WrongRepr())
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
154
39544
01034018e8b2 Make the it_index field in the str/unicode iterators Py_ssize_t's.
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
155 def test_iterators(self):
01034018e8b2 Make the it_index field in the str/unicode iterators Py_ssize_t's.
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
156 # Make sure unicode objects have an __iter__ method
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
157 it = "\u1111\u2222\u3333".__iter__()
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
158 self.assertEqual(next(it), "\u1111")
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
159 self.assertEqual(next(it), "\u2222")
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
160 self.assertEqual(next(it), "\u3333")
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41480
diff changeset
161 self.assertRaises(StopIteration, next, it)
39544
01034018e8b2 Make the it_index field in the str/unicode iterators Py_ssize_t's.
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
162
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
163 def test_count(self):
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
164 string_tests.CommonTest.test_count(self)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
165 # check mixed argument types
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
166 self.checkequalnofix(3, 'aaa', 'count', 'a')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
167 self.checkequalnofix(0, 'aaa', 'count', 'b')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
168 self.checkequalnofix(3, 'aaa', 'count', 'a')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
169 self.checkequalnofix(0, 'aaa', 'count', 'b')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
170 self.checkequalnofix(0, 'aaa', 'count', 'b')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
171 self.checkequalnofix(1, 'aaa', 'count', 'a', -1)
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
172 self.checkequalnofix(3, 'aaa', 'count', 'a', -10)
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
173 self.checkequalnofix(2, 'aaa', 'count', 'a', 0, -1)
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
174 self.checkequalnofix(0, 'aaa', 'count', 'a', 0, -10)
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
176 def test_find(self):
72813
5ae50a57dd91 test_unicode was forgetting to run the common string tests for str.find()
Antoine Pitrou <solipsis@pitrou.net>
parents: 72040
diff changeset
177 string_tests.CommonTest.test_find(self)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
178 self.checkequalnofix(0, 'abcdefghiabc', 'find', 'abc')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
179 self.checkequalnofix(9, 'abcdefghiabc', 'find', 'abc', 1)
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
180 self.checkequalnofix(-1, 'abcdefghiabc', 'find', 'def', 4)
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
181
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
182 self.assertRaises(TypeError, 'hello'.find)
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
183 self.assertRaises(TypeError, 'hello'.find, 42)
15910
f7f3be3d00b9 Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents: 15706
diff changeset
184
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
185 def test_rfind(self):
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
186 string_tests.CommonTest.test_rfind(self)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
187 # check mixed argument types
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
188 self.checkequalnofix(9, 'abcdefghiabc', 'rfind', 'abc')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
189 self.checkequalnofix(12, 'abcdefghiabc', 'rfind', '')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
190 self.checkequalnofix(12, 'abcdefghiabc', 'rfind', '')
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
192 def test_index(self):
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
193 string_tests.CommonTest.test_index(self)
41592
1e5a2a2b18ee Fix various spots where int/long and str/unicode unification
Walter Dörwald <walter@livinglogic.de>
parents: 41585
diff changeset
194 self.checkequalnofix(0, 'abcdefghiabc', 'index', '')
1e5a2a2b18ee Fix various spots where int/long and str/unicode unification
Walter Dörwald <walter@livinglogic.de>
parents: 41585
diff changeset
195 self.checkequalnofix(3, 'abcdefghiabc', 'index', 'def')
1e5a2a2b18ee Fix various spots where int/long and str/unicode unification
Walter Dörwald <walter@livinglogic.de>
parents: 41585
diff changeset
196 self.checkequalnofix(0, 'abcdefghiabc', 'index', 'abc')
1e5a2a2b18ee Fix various spots where int/long and str/unicode unification
Walter Dörwald <walter@livinglogic.de>
parents: 41585
diff changeset
197 self.checkequalnofix(9, 'abcdefghiabc', 'index', 'abc', 1)
1e5a2a2b18ee Fix various spots where int/long and str/unicode unification
Walter Dörwald <walter@livinglogic.de>
parents: 41585
diff changeset
198 self.assertRaises(ValueError, 'abcdefghiabc'.index, 'hib')
1e5a2a2b18ee Fix various spots where int/long and str/unicode unification
Walter Dörwald <walter@livinglogic.de>
parents: 41585
diff changeset
199 self.assertRaises(ValueError, 'abcdefghiab'.index, 'abc', 1)
1e5a2a2b18ee Fix various spots where int/long and str/unicode unification
Walter Dörwald <walter@livinglogic.de>
parents: 41585
diff changeset
200 self.assertRaises(ValueError, 'abcdefghi'.index, 'ghi', 8)
1e5a2a2b18ee Fix various spots where int/long and str/unicode unification
Walter Dörwald <walter@livinglogic.de>
parents: 41585
diff changeset
201 self.assertRaises(ValueError, 'abcdefghi'.index, 'ghi', -1)
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
203 def test_rindex(self):
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
204 string_tests.CommonTest.test_rindex(self)
41592
1e5a2a2b18ee Fix various spots where int/long and str/unicode unification
Walter Dörwald <walter@livinglogic.de>
parents: 41585
diff changeset
205 self.checkequalnofix(12, 'abcdefghiabc', 'rindex', '')
1e5a2a2b18ee Fix various spots where int/long and str/unicode unification
Walter Dörwald <walter@livinglogic.de>
parents: 41585
diff changeset
206 self.checkequalnofix(3, 'abcdefghiabc', 'rindex', 'def')
1e5a2a2b18ee Fix various spots where int/long and str/unicode unification
Walter Dörwald <walter@livinglogic.de>
parents: 41585
diff changeset
207 self.checkequalnofix(9, 'abcdefghiabc', 'rindex', 'abc')
1e5a2a2b18ee Fix various spots where int/long and str/unicode unification
Walter Dörwald <walter@livinglogic.de>
parents: 41585
diff changeset
208 self.checkequalnofix(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209
41592
1e5a2a2b18ee Fix various spots where int/long and str/unicode unification
Walter Dörwald <walter@livinglogic.de>
parents: 41585
diff changeset
210 self.assertRaises(ValueError, 'abcdefghiabc'.rindex, 'hib')
1e5a2a2b18ee Fix various spots where int/long and str/unicode unification
Walter Dörwald <walter@livinglogic.de>
parents: 41585
diff changeset
211 self.assertRaises(ValueError, 'defghiabc'.rindex, 'def', 1)
1e5a2a2b18ee Fix various spots where int/long and str/unicode unification
Walter Dörwald <walter@livinglogic.de>
parents: 41585
diff changeset
212 self.assertRaises(ValueError, 'defghiabc'.rindex, 'abc', 0, -1)
1e5a2a2b18ee Fix various spots where int/long and str/unicode unification
Walter Dörwald <walter@livinglogic.de>
parents: 41585
diff changeset
213 self.assertRaises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, 8)
1e5a2a2b18ee Fix various spots where int/long and str/unicode unification
Walter Dörwald <walter@livinglogic.de>
parents: 41585
diff changeset
214 self.assertRaises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, -1)
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
215
44039
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
216 def test_maketrans_translate(self):
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
217 # these work with plain translate()
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
218 self.checkequalnofix('bbbc', 'abababc', 'translate',
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
219 {ord('a'): None})
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
220 self.checkequalnofix('iiic', 'abababc', 'translate',
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
221 {ord('a'): None, ord('b'): ord('i')})
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
222 self.checkequalnofix('iiix', 'abababc', 'translate',
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
223 {ord('a'): None, ord('b'): ord('i'), ord('c'): 'x'})
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
224 self.checkequalnofix('c', 'abababc', 'translate',
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
225 {ord('a'): None, ord('b'): ''})
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
226 self.checkequalnofix('xyyx', 'xzx', 'translate',
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
227 {ord('z'): 'yy'})
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
228 # this needs maketrans()
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
229 self.checkequalnofix('abababc', 'abababc', 'translate',
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
230 {'b': '<i>'})
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
231 tbl = self.type2test.maketrans({'a': None, 'b': '<i>'})
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
232 self.checkequalnofix('<i><i><i>c', 'abababc', 'translate', tbl)
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
233 # test alternative way of calling maketrans()
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
234 tbl = self.type2test.maketrans('abc', 'xyz', 'd')
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
235 self.checkequalnofix('xyzzy', 'abdcdcbdddd', 'translate', tbl)
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
236
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
237 self.assertRaises(TypeError, self.type2test.maketrans)
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
238 self.assertRaises(ValueError, self.type2test.maketrans, 'abc', 'defg')
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
239 self.assertRaises(TypeError, self.type2test.maketrans, 2, 'def')
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
240 self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 2)
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
241 self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 'def', 2)
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
242 self.assertRaises(ValueError, self.type2test.maketrans, {'xy': 2})
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
243 self.assertRaises(TypeError, self.type2test.maketrans, {(1,): 2})
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
244
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
245 self.assertRaises(TypeError, 'hello'.translate)
41620
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
246 self.assertRaises(TypeError, 'abababc'.translate, 'abc', 'xyz')
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
248 def test_split(self):
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
249 string_tests.CommonTest.test_split(self)
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
250
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
251 # Mixed arguments
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
252 self.checkequalnofix(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
253 self.checkequalnofix(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
254 self.checkequalnofix(['endcase ', ''], 'endcase test', 'split', 'test')
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
255
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
256 def test_join(self):
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
257 string_tests.MixinStrUnicodeUserStringTest.test_join(self)
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23115
diff changeset
258
43446
a7c48ab809ec Patch # 1145 by Thomas Lee:
Guido van Rossum <guido@python.org>
parents: 43241
diff changeset
259 class MyWrapper:
a7c48ab809ec Patch # 1145 by Thomas Lee:
Guido van Rossum <guido@python.org>
parents: 43241
diff changeset
260 def __init__(self, sval): self.sval = sval
a7c48ab809ec Patch # 1145 by Thomas Lee:
Guido van Rossum <guido@python.org>
parents: 43241
diff changeset
261 def __str__(self): return self.sval
a7c48ab809ec Patch # 1145 by Thomas Lee:
Guido van Rossum <guido@python.org>
parents: 43241
diff changeset
262
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
263 # mixed arguments
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
264 self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
265 self.checkequalnofix('abcd', '', 'join', ('a', 'b', 'c', 'd'))
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
266 self.checkequalnofix('w x y z', ' ', 'join', string_tests.Sequence('wxyz'))
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
267 self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
268 self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
269 self.checkequalnofix('abcd', '', 'join', ('a', 'b', 'c', 'd'))
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
270 self.checkequalnofix('w x y z', ' ', 'join', string_tests.Sequence('wxyz'))
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
271 self.checkraises(TypeError, ' ', 'join', ['1', '2', MyWrapper('foo')])
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
272 self.checkraises(TypeError, ' ', 'join', ['1', '2', '3', bytes()])
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
273 self.checkraises(TypeError, ' ', 'join', [1, 2, 3])
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
274 self.checkraises(TypeError, ' ', 'join', ['1', '2', 3])
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
275
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
276 def test_replace(self):
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
277 string_tests.CommonTest.test_replace(self)
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
278
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
279 # method call forwarded from str implementation because of unicode argument
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
280 self.checkequalnofix('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
281 self.assertRaises(TypeError, 'replace'.replace, "r", 42)
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
282
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
283 def test_bytes_comparison(self):
59775
e619047a2f36 Clean up the warnings filter use in test_unicode.
Brett Cannon <bcannon@gmail.com>
parents: 58415
diff changeset
284 with support.check_warnings():
e619047a2f36 Clean up the warnings filter use in test_unicode.
Brett Cannon <bcannon@gmail.com>
parents: 58415
diff changeset
285 warnings.simplefilter('ignore', BytesWarning)
e619047a2f36 Clean up the warnings filter use in test_unicode.
Brett Cannon <bcannon@gmail.com>
parents: 58415
diff changeset
286 self.assertEqual('abc' == b'abc', False)
e619047a2f36 Clean up the warnings filter use in test_unicode.
Brett Cannon <bcannon@gmail.com>
parents: 58415
diff changeset
287 self.assertEqual('abc' != b'abc', True)
e619047a2f36 Clean up the warnings filter use in test_unicode.
Brett Cannon <bcannon@gmail.com>
parents: 58415
diff changeset
288 self.assertEqual('abc' == bytearray(b'abc'), False)
e619047a2f36 Clean up the warnings filter use in test_unicode.
Brett Cannon <bcannon@gmail.com>
parents: 58415
diff changeset
289 self.assertEqual('abc' != bytearray(b'abc'), True)
43638
b10544827af7 Make str/str8 comparisons return True/False for !=/==.
Brett Cannon <bcannon@gmail.com>
parents: 43487
diff changeset
290
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
291 def test_comparison(self):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
292 # Comparisons:
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
293 self.assertEqual('abc', 'abc')
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54484
diff changeset
294 self.assertTrue('abcd' > 'abc')
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54484
diff changeset
295 self.assertTrue('abc' < 'abcd')
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
296
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
297 if 0:
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
298 # Move these tests to a Unicode collation module test...
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
299 # Testing UTF-16 code point order comparisons...
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
300
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
301 # No surrogates, no fixup required.
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54484
diff changeset
302 self.assertTrue('\u0061' < '\u20ac')
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
303 # Non surrogate below surrogate value, no fixup required
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54484
diff changeset
304 self.assertTrue('\u0061' < '\ud800\udc02')
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
305
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
306 # Non surrogate above surrogate value, fixup required
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
307 def test_lecmp(s, s2):
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54484
diff changeset
308 self.assertTrue(s < s2)
13410
5157eb7be20d Tests for new surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13400
diff changeset
309
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
310 def test_fixup(s):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
311 s2 = '\ud800\udc01'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
312 test_lecmp(s, s2)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
313 s2 = '\ud900\udc01'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
314 test_lecmp(s, s2)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
315 s2 = '\uda00\udc01'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
316 test_lecmp(s, s2)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
317 s2 = '\udb00\udc01'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
318 test_lecmp(s, s2)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
319 s2 = '\ud800\udd01'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
320 test_lecmp(s, s2)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
321 s2 = '\ud900\udd01'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
322 test_lecmp(s, s2)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
323 s2 = '\uda00\udd01'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
324 test_lecmp(s, s2)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
325 s2 = '\udb00\udd01'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
326 test_lecmp(s, s2)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
327 s2 = '\ud800\ude01'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
328 test_lecmp(s, s2)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
329 s2 = '\ud900\ude01'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
330 test_lecmp(s, s2)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
331 s2 = '\uda00\ude01'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
332 test_lecmp(s, s2)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
333 s2 = '\udb00\ude01'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
334 test_lecmp(s, s2)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
335 s2 = '\ud800\udfff'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
336 test_lecmp(s, s2)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
337 s2 = '\ud900\udfff'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
338 test_lecmp(s, s2)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
339 s2 = '\uda00\udfff'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
340 test_lecmp(s, s2)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
341 s2 = '\udb00\udfff'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
342 test_lecmp(s, s2)
13410
5157eb7be20d Tests for new surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13400
diff changeset
343
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
344 test_fixup('\ue000')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
345 test_fixup('\uff61')
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
346
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
347 # Surrogates on both sides, no fixup required
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54484
diff changeset
348 self.assertTrue('\ud800\udc02' < '\ud84d\udc56')
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
349
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
350 def test_islower(self):
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
351 string_tests.MixinStrUnicodeUserStringTest.test_islower(self)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
352 self.checkequalnofix(False, '\u1FFc', 'islower')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
353 # non-BMP, uppercase
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
354 self.assertFalse('\U00010401'.islower())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
355 self.assertFalse('\U00010427'.islower())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
356 # non-BMP, lowercase
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
357 self.assertTrue('\U00010429'.islower())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
358 self.assertTrue('\U0001044E'.islower())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
359 # non-BMP, non-cased
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
360 self.assertFalse('\U0001F40D'.islower())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
361 self.assertFalse('\U0001F46F'.islower())
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
362
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
363 def test_isupper(self):
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
364 string_tests.MixinStrUnicodeUserStringTest.test_isupper(self)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
365 if not sys.platform.startswith('java'):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
366 self.checkequalnofix(False, '\u1FFc', 'isupper')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
367 # non-BMP, uppercase
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
368 self.assertTrue('\U00010401'.isupper())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
369 self.assertTrue('\U00010427'.isupper())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
370 # non-BMP, lowercase
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
371 self.assertFalse('\U00010429'.isupper())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
372 self.assertFalse('\U0001044E'.isupper())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
373 # non-BMP, non-cased
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
374 self.assertFalse('\U0001F40D'.isupper())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
375 self.assertFalse('\U0001F46F'.isupper())
13353
0b289e00d52a Added tests for the new .isalpha() and .isalnum() methods.
Marc-André Lemburg <mal@egenix.com>
parents: 13189
diff changeset
376
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
377 def test_istitle(self):
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
378 string_tests.MixinStrUnicodeUserStringTest.test_istitle(self)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
379 self.checkequalnofix(True, '\u1FFc', 'istitle')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
380 self.checkequalnofix(True, 'Greek \u1FFcitlecases ...', 'istitle')
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
381
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
382 # non-BMP, uppercase + lowercase
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
383 self.assertTrue('\U00010401\U00010429'.istitle())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
384 self.assertTrue('\U00010427\U0001044E'.istitle())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
385 # apparently there are no titlecased (Lt) non-BMP chars in Unicode 6
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
386 for ch in ['\U00010429', '\U0001044E', '\U0001F40D', '\U0001F46F']:
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
387 self.assertFalse(ch.istitle(), '{!a} is not title'.format(ch))
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
388
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
389 def test_isspace(self):
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
390 string_tests.MixinStrUnicodeUserStringTest.test_isspace(self)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
391 self.checkequalnofix(True, '\u2000', 'isspace')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
392 self.checkequalnofix(True, '\u200a', 'isspace')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
393 self.checkequalnofix(False, '\u2014', 'isspace')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
394 # apparently there are no non-BMP spaces chars in Unicode 6
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
395 for ch in ['\U00010401', '\U00010427', '\U00010429', '\U0001044E',
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
396 '\U0001F40D', '\U0001F46F']:
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
397 self.assertFalse(ch.isspace(), '{!a} is not space.'.format(ch))
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
398
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
399 def test_isalnum(self):
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
400 string_tests.MixinStrUnicodeUserStringTest.test_isalnum(self)
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
401 for ch in ['\U00010401', '\U00010427', '\U00010429', '\U0001044E',
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
402 '\U0001D7F6', '\U00011066', '\U000104A0', '\U0001F107']:
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
403 self.assertTrue(ch.isalnum(), '{!a} is alnum.'.format(ch))
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
404
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
405 def test_isalpha(self):
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
406 string_tests.MixinStrUnicodeUserStringTest.test_isalpha(self)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
407 self.checkequalnofix(True, '\u1FFc', 'isalpha')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
408 # non-BMP, cased
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
409 self.assertTrue('\U00010401'.isalpha())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
410 self.assertTrue('\U00010427'.isalpha())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
411 self.assertTrue('\U00010429'.isalpha())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
412 self.assertTrue('\U0001044E'.isalpha())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
413 # non-BMP, non-cased
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
414 self.assertFalse('\U0001F40D'.isalpha())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
415 self.assertFalse('\U0001F46F'.isalpha())
12001
e362f326048c Marc-Andre Lemburg: Add tests for mixed use of char in string.
Guido van Rossum <guido@python.org>
parents: 11992
diff changeset
416
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
417 def test_isdecimal(self):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
418 self.checkequalnofix(False, '', 'isdecimal')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
419 self.checkequalnofix(False, 'a', 'isdecimal')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
420 self.checkequalnofix(True, '0', 'isdecimal')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
421 self.checkequalnofix(False, '\u2460', 'isdecimal') # CIRCLED DIGIT ONE
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
422 self.checkequalnofix(False, '\xbc', 'isdecimal') # VULGAR FRACTION ONE QUARTER
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
423 self.checkequalnofix(True, '\u0660', 'isdecimal') # ARABIC-INDIC DIGIT ZERO
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
424 self.checkequalnofix(True, '0123456789', 'isdecimal')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
425 self.checkequalnofix(False, '0123456789a', 'isdecimal')
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
426
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
427 self.checkraises(TypeError, 'abc', 'isdecimal', 42)
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
428
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
429 for ch in ['\U00010401', '\U00010427', '\U00010429', '\U0001044E',
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
430 '\U0001F40D', '\U0001F46F', '\U00011065', '\U0001F107']:
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
431 self.assertFalse(ch.isdecimal(), '{!a} is not decimal.'.format(ch))
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
432 for ch in ['\U0001D7F6', '\U00011066', '\U000104A0']:
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
433 self.assertTrue(ch.isdecimal(), '{!a} is decimal.'.format(ch))
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
434
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
435 def test_isdigit(self):
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
436 string_tests.MixinStrUnicodeUserStringTest.test_isdigit(self)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
437 self.checkequalnofix(True, '\u2460', 'isdigit')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
438 self.checkequalnofix(False, '\xbc', 'isdigit')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
439 self.checkequalnofix(True, '\u0660', 'isdigit')
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
440
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
441 for ch in ['\U00010401', '\U00010427', '\U00010429', '\U0001044E',
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
442 '\U0001F40D', '\U0001F46F', '\U00011065']:
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
443 self.assertFalse(ch.isdigit(), '{!a} is not a digit.'.format(ch))
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
444 for ch in ['\U0001D7F6', '\U00011066', '\U000104A0', '\U0001F107']:
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
445 self.assertTrue(ch.isdigit(), '{!a} is a digit.'.format(ch))
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
446
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
447 def test_isnumeric(self):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
448 self.checkequalnofix(False, '', 'isnumeric')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
449 self.checkequalnofix(False, 'a', 'isnumeric')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
450 self.checkequalnofix(True, '0', 'isnumeric')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
451 self.checkequalnofix(True, '\u2460', 'isnumeric')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
452 self.checkequalnofix(True, '\xbc', 'isnumeric')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
453 self.checkequalnofix(True, '\u0660', 'isnumeric')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
454 self.checkequalnofix(True, '0123456789', 'isnumeric')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
455 self.checkequalnofix(False, '0123456789a', 'isnumeric')
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
456
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
457 self.assertRaises(TypeError, "abc".isnumeric, 42)
12917
2a2caaee6797 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12897
diff changeset
458
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
459 for ch in ['\U00010401', '\U00010427', '\U00010429', '\U0001044E',
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
460 '\U0001F40D', '\U0001F46F']:
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
461 self.assertFalse(ch.isnumeric(), '{!a} is not numeric.'.format(ch))
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
462 for ch in ['\U00011065', '\U0001D7F6', '\U00011066',
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
463 '\U000104A0', '\U0001F107']:
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
464 self.assertTrue(ch.isnumeric(), '{!a} is numeric.'.format(ch))
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
465
42597
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
466 def test_isidentifier(self):
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
467 self.assertTrue("a".isidentifier())
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
468 self.assertTrue("Z".isidentifier())
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
469 self.assertTrue("_".isidentifier())
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
470 self.assertTrue("b0".isidentifier())
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
471 self.assertTrue("bc".isidentifier())
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
472 self.assertTrue("b_".isidentifier())
65661
e9e3fe4c9e30 Merged revisions 85861 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63941
diff changeset
473 self.assertTrue("µ".isidentifier())
71848
787ed1a7aba8 in narrow builds, make sure to test codepoints as identifier characters (closes #12732)
Benjamin Peterson <benjamin@python.org>
parents: 71409
diff changeset
474 self.assertTrue("𝔘𝔫𝔦𝔠𝔬𝔡𝔢".isidentifier())
42597
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
475
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
476 self.assertFalse(" ".isidentifier())
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
477 self.assertFalse("[".isidentifier())
65661
e9e3fe4c9e30 Merged revisions 85861 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63941
diff changeset
478 self.assertFalse("©".isidentifier())
48427
4e6859c6d38c Issue #3282: str.isprintable() should return False for undefined Unicode characters.
Georg Brandl <georg@python.org>
parents: 47611
diff changeset
479 self.assertFalse("0".isidentifier())
42597
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
480
47611
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
481 def test_isprintable(self):
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
482 self.assertTrue("".isprintable())
52345
07b6fcdf7f94 fix isprintable() on space characters #5126
Benjamin Peterson <benjamin@python.org>
parents: 52159
diff changeset
483 self.assertTrue(" ".isprintable())
47611
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
484 self.assertTrue("abcdefg".isprintable())
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
485 self.assertFalse("abcdefg\n".isprintable())
48427
4e6859c6d38c Issue #3282: str.isprintable() should return False for undefined Unicode characters.
Georg Brandl <georg@python.org>
parents: 47611
diff changeset
486 # some defined Unicode character
4e6859c6d38c Issue #3282: str.isprintable() should return False for undefined Unicode characters.
Georg Brandl <georg@python.org>
parents: 47611
diff changeset
487 self.assertTrue("\u0374".isprintable())
4e6859c6d38c Issue #3282: str.isprintable() should return False for undefined Unicode characters.
Georg Brandl <georg@python.org>
parents: 47611
diff changeset
488 # undefined character
49142
51ff934562c2 The Unicode database was updated to 5.1, and some characters have become printable.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49032
diff changeset
489 self.assertFalse("\u0378".isprintable())
48427
4e6859c6d38c Issue #3282: str.isprintable() should return False for undefined Unicode characters.
Georg Brandl <georg@python.org>
parents: 47611
diff changeset
490 # single surrogate character
47611
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
491 self.assertFalse("\ud800".isprintable())
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
492
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
493 self.assertTrue('\U0001F46F'.isprintable())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
494 self.assertFalse('\U000E0020'.isprintable())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
495
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
496 def test_surrogates(self):
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
497 for s in ('a\uD800b\uDFFF', 'a\uDFFFb\uD800',
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
498 'a\uD800b\uDFFFa', 'a\uDFFFb\uD800a'):
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
499 self.assertTrue(s.islower())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
500 self.assertFalse(s.isupper())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
501 self.assertFalse(s.istitle())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
502 for s in ('A\uD800B\uDFFF', 'A\uDFFFB\uD800',
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
503 'A\uD800B\uDFFFA', 'A\uDFFFB\uD800A'):
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
504 self.assertFalse(s.islower())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
505 self.assertTrue(s.isupper())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
506 self.assertTrue(s.istitle())
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
507
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
508 for meth_name in ('islower', 'isupper', 'istitle'):
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
509 meth = getattr(str, meth_name)
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
510 for s in ('\uD800', '\uDFFF', '\uD800\uD800', '\uDFFF\uDFFF'):
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
511 self.assertFalse(meth(s), '%a.%s() is False' % (s, meth_name))
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
512
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
513 for meth_name in ('isalpha', 'isalnum', 'isdigit', 'isspace',
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
514 'isdecimal', 'isnumeric',
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
515 'isidentifier', 'isprintable'):
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
516 meth = getattr(str, meth_name)
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
517 for s in ('\uD800', '\uDFFF', '\uD800\uD800', '\uDFFF\uDFFF',
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
518 'a\uD800b\uDFFF', 'a\uDFFFb\uD800',
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
519 'a\uD800b\uDFFFa', 'a\uDFFFb\uD800a'):
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
520 self.assertFalse(meth(s), '%a.%s() is False' % (s, meth_name))
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
521
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
522
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
523 @requires_wide_build
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
524 def test_lower(self):
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
525 string_tests.CommonTest.test_lower(self)
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
526 self.assertEqual('\U00010427'.lower(), '\U0001044F')
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
527 self.assertEqual('\U00010427\U00010427'.lower(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
528 '\U0001044F\U0001044F')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
529 self.assertEqual('\U00010427\U0001044F'.lower(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
530 '\U0001044F\U0001044F')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
531 self.assertEqual('X\U00010427x\U0001044F'.lower(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
532 'x\U0001044Fx\U0001044F')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
533
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
534 @requires_wide_build
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
535 def test_upper(self):
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
536 string_tests.CommonTest.test_upper(self)
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
537 self.assertEqual('\U0001044F'.upper(), '\U00010427')
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
538 self.assertEqual('\U0001044F\U0001044F'.upper(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
539 '\U00010427\U00010427')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
540 self.assertEqual('\U00010427\U0001044F'.upper(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
541 '\U00010427\U00010427')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
542 self.assertEqual('X\U00010427x\U0001044F'.upper(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
543 'X\U00010427X\U00010427')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
544
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
545 @requires_wide_build
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
546 def test_capitalize(self):
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
547 string_tests.CommonTest.test_capitalize(self)
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
548 self.assertEqual('\U0001044F'.capitalize(), '\U00010427')
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
549 self.assertEqual('\U0001044F\U0001044F'.capitalize(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
550 '\U00010427\U0001044F')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
551 self.assertEqual('\U00010427\U0001044F'.capitalize(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
552 '\U00010427\U0001044F')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
553 self.assertEqual('\U0001044F\U00010427'.capitalize(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
554 '\U00010427\U0001044F')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
555 self.assertEqual('X\U00010427x\U0001044F'.capitalize(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
556 'X\U0001044Fx\U0001044F')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
557
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
558 @requires_wide_build
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
559 def test_title(self):
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
560 string_tests.MixinStrUnicodeUserStringTest.test_title(self)
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
561 self.assertEqual('\U0001044F'.title(), '\U00010427')
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
562 self.assertEqual('\U0001044F\U0001044F'.title(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
563 '\U00010427\U0001044F')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
564 self.assertEqual('\U0001044F\U0001044F \U0001044F\U0001044F'.title(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
565 '\U00010427\U0001044F \U00010427\U0001044F')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
566 self.assertEqual('\U00010427\U0001044F \U00010427\U0001044F'.title(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
567 '\U00010427\U0001044F \U00010427\U0001044F')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
568 self.assertEqual('\U0001044F\U00010427 \U0001044F\U00010427'.title(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
569 '\U00010427\U0001044F \U00010427\U0001044F')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
570 self.assertEqual('X\U00010427x\U0001044F X\U00010427x\U0001044F'.title(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
571 'X\U0001044Fx\U0001044F X\U0001044Fx\U0001044F')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
572
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
573 @requires_wide_build
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
574 def test_swapcase(self):
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
575 string_tests.CommonTest.test_swapcase(self)
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
576 self.assertEqual('\U0001044F'.swapcase(), '\U00010427')
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
577 self.assertEqual('\U00010427'.swapcase(), '\U0001044F')
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
578 self.assertEqual('\U0001044F\U0001044F'.swapcase(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
579 '\U00010427\U00010427')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
580 self.assertEqual('\U00010427\U0001044F'.swapcase(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
581 '\U0001044F\U00010427')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
582 self.assertEqual('\U0001044F\U00010427'.swapcase(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
583 '\U00010427\U0001044F')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
584 self.assertEqual('X\U00010427x\U0001044F'.swapcase(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
585 'x\U0001044FX\U00010427')
72035
06b30c5bcc3d #9200: The str.is* methods now work with strings that contain non-BMP characters even in narrow Unicode builds.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71848
diff changeset
586
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
587 def test_contains(self):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
588 # Testing Unicode contains method
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
589 self.assertIn('a', 'abdb')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
590 self.assertIn('a', 'bdab')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
591 self.assertIn('a', 'bdaba')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
592 self.assertIn('a', 'bdba')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
593 self.assertNotIn('a', 'bdb')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
594 self.assertIn('a', 'bdba')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
595 self.assertIn('a', ('a',1,None))
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
596 self.assertIn('a', (1,None,'a'))
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
597 self.assertIn('a', ('a',1,None))
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
598 self.assertIn('a', (1,None,'a'))
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
599 self.assertNotIn('a', ('x',1,'y'))
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
600 self.assertNotIn('a', ('x',1,None))
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
601 self.assertNotIn('abcd', 'abcxxxx')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
602 self.assertIn('ab', 'abcd')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
603 self.assertIn('ab', 'abc')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
604 self.assertIn('ab', (1,None,'ab'))
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
605 self.assertIn('', 'abc')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
606 self.assertIn('', '')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
607 self.assertIn('', 'abc')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
608 self.assertNotIn('\0', 'abc')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
609 self.assertIn('\0', '\0abc')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
610 self.assertIn('\0', 'abc\0')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
611 self.assertIn('a', '\0abc')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
612 self.assertIn('asdf', 'asdf')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
613 self.assertNotIn('asdf', 'asd')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
614 self.assertNotIn('asdf', '')
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
615
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
616 self.assertRaises(TypeError, "abc".__contains__)
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
617
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
618 def test_format(self):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
619 self.assertEqual(''.format(), '')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
620 self.assertEqual('a'.format(), 'a')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
621 self.assertEqual('ab'.format(), 'ab')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
622 self.assertEqual('a{{'.format(), 'a{')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
623 self.assertEqual('a}}'.format(), 'a}')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
624 self.assertEqual('{{b'.format(), '{b')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
625 self.assertEqual('}}b'.format(), '}b')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
626 self.assertEqual('a{{b'.format(), 'a{b')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
627
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
628 # examples from the PEP:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
629 import datetime
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
630 self.assertEqual("My name is {0}".format('Fred'), "My name is Fred")
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
631 self.assertEqual("My name is {0[name]}".format(dict(name='Fred')),
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
632 "My name is Fred")
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
633 self.assertEqual("My name is {0} :-{{}}".format('Fred'),
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
634 "My name is Fred :-{}")
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
635
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
636 d = datetime.date(2007, 8, 18)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
637 self.assertEqual("The year is {0.year}".format(d),
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
638 "The year is 2007")
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
639
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
640 # classes we'll use for testing
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
641 class C:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
642 def __init__(self, x=100):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
643 self._x = x
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
644 def __format__(self, spec):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
645 return spec
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
646
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
647 class D:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
648 def __init__(self, x):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
649 self.x = x
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
650 def __format__(self, spec):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
651 return str(self.x)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
652
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
653 # class with __str__, but no __format__
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
654 class E:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
655 def __init__(self, x):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
656 self.x = x
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
657 def __str__(self):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
658 return 'E(' + self.x + ')'
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
659
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
660 # class with __repr__, but no __format__ or __str__
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
661 class F:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
662 def __init__(self, x):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
663 self.x = x
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
664 def __repr__(self):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
665 return 'F(' + self.x + ')'
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
666
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
667 # class with __format__ that forwards to string, for some format_spec's
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
668 class G:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
669 def __init__(self, x):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
670 self.x = x
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
671 def __str__(self):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
672 return "string is " + self.x
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
673 def __format__(self, format_spec):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
674 if format_spec == 'd':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
675 return 'G(' + self.x + ')'
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
676 return object.__format__(self, format_spec)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
677
42909
7a62c2469dea Additional test for formatting code.
Eric Smith <eric@trueblade.com>
parents: 42907
diff changeset
678 class I(datetime.date):
7a62c2469dea Additional test for formatting code.
Eric Smith <eric@trueblade.com>
parents: 42907
diff changeset
679 def __format__(self, format_spec):
7a62c2469dea Additional test for formatting code.
Eric Smith <eric@trueblade.com>
parents: 42907
diff changeset
680 return self.strftime(format_spec)
7a62c2469dea Additional test for formatting code.
Eric Smith <eric@trueblade.com>
parents: 42907
diff changeset
681
43097
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42909
diff changeset
682 class J(int):
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42909
diff changeset
683 def __format__(self, format_spec):
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42909
diff changeset
684 return int.__format__(self * 2, format_spec)
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42909
diff changeset
685
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
686
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
687 self.assertEqual(''.format(), '')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
688 self.assertEqual('abc'.format(), 'abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
689 self.assertEqual('{0}'.format('abc'), 'abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
690 self.assertEqual('{0:}'.format('abc'), 'abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
691 # self.assertEqual('{ 0 }'.format('abc'), 'abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
692 self.assertEqual('X{0}'.format('abc'), 'Xabc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
693 self.assertEqual('{0}X'.format('abc'), 'abcX')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
694 self.assertEqual('X{0}Y'.format('abc'), 'XabcY')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
695 self.assertEqual('{1}'.format(1, 'abc'), 'abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
696 self.assertEqual('X{1}'.format(1, 'abc'), 'Xabc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
697 self.assertEqual('{1}X'.format(1, 'abc'), 'abcX')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
698 self.assertEqual('X{1}Y'.format(1, 'abc'), 'XabcY')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
699 self.assertEqual('{0}'.format(-15), '-15')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
700 self.assertEqual('{0}{1}'.format(-15, 'abc'), '-15abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
701 self.assertEqual('{0}X{1}'.format(-15, 'abc'), '-15Xabc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
702 self.assertEqual('{{'.format(), '{')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
703 self.assertEqual('}}'.format(), '}')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
704 self.assertEqual('{{}}'.format(), '{}')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
705 self.assertEqual('{{x}}'.format(), '{x}')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
706 self.assertEqual('{{{0}}}'.format(123), '{123}')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
707 self.assertEqual('{{{{0}}}}'.format(), '{{0}}')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
708 self.assertEqual('}}{{'.format(), '}{')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
709 self.assertEqual('}}x{{'.format(), '}x{')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
710
42875
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
711 # weird field names
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
712 self.assertEqual("{0[foo-bar]}".format({'foo-bar':'baz'}), 'baz')
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
713 self.assertEqual("{0[foo bar]}".format({'foo bar':'baz'}), 'baz')
43206
f4fb74f63806 Fix segfault discovered by Ron Adam. Not checking for terminating right bracket in "'{0[}'.format(())". Fixed, and tests added.
Eric Smith <eric@trueblade.com>
parents: 43172
diff changeset
714 self.assertEqual("{0[ ]}".format({' ':3}), '3')
42875
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
715
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
716 self.assertEqual('{foo._x}'.format(foo=C(20)), '20')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
717 self.assertEqual('{1}{0}'.format(D(10), D(20)), '2010')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
718 self.assertEqual('{0._x.x}'.format(C(D('abc'))), 'abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
719 self.assertEqual('{0[0]}'.format(['abc', 'def']), 'abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
720 self.assertEqual('{0[1]}'.format(['abc', 'def']), 'def')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
721 self.assertEqual('{0[1][0]}'.format(['abc', ['def']]), 'def')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
722 self.assertEqual('{0[1][0].x}'.format(['abc', [D('def')]]), 'def')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
723
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
724 # strings
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
725 self.assertEqual('{0:.3s}'.format('abc'), 'abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
726 self.assertEqual('{0:.3s}'.format('ab'), 'ab')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
727 self.assertEqual('{0:.3s}'.format('abcdef'), 'abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
728 self.assertEqual('{0:.0s}'.format('abcdef'), '')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
729 self.assertEqual('{0:3.3s}'.format('abc'), 'abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
730 self.assertEqual('{0:2.3s}'.format('abc'), 'abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
731 self.assertEqual('{0:2.2s}'.format('abc'), 'ab')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
732 self.assertEqual('{0:3.2s}'.format('abc'), 'ab ')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
733 self.assertEqual('{0:x<0s}'.format('result'), 'result')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
734 self.assertEqual('{0:x<5s}'.format('result'), 'result')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
735 self.assertEqual('{0:x<6s}'.format('result'), 'result')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
736 self.assertEqual('{0:x<7s}'.format('result'), 'resultx')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
737 self.assertEqual('{0:x<8s}'.format('result'), 'resultxx')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
738 self.assertEqual('{0: <7s}'.format('result'), 'result ')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
739 self.assertEqual('{0:<7s}'.format('result'), 'result ')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
740 self.assertEqual('{0:>7s}'.format('result'), ' result')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
741 self.assertEqual('{0:>8s}'.format('result'), ' result')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
742 self.assertEqual('{0:^8s}'.format('result'), ' result ')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
743 self.assertEqual('{0:^9s}'.format('result'), ' result ')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
744 self.assertEqual('{0:^10s}'.format('result'), ' result ')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
745 self.assertEqual('{0:10000}'.format('a'), 'a' + ' ' * 9999)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
746 self.assertEqual('{0:10000}'.format(''), ' ' * 10000)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
747 self.assertEqual('{0:10000000}'.format(''), ' ' * 10000000)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
748
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
749 # format specifiers for user defined type
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
750 self.assertEqual('{0:abc}'.format(C()), 'abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
751
48427
4e6859c6d38c Issue #3282: str.isprintable() should return False for undefined Unicode characters.
Georg Brandl <georg@python.org>
parents: 47611
diff changeset
752 # !r, !s and !a coercions
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
753 self.assertEqual('{0!s}'.format('Hello'), 'Hello')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
754 self.assertEqual('{0!s:}'.format('Hello'), 'Hello')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
755 self.assertEqual('{0!s:15}'.format('Hello'), 'Hello ')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
756 self.assertEqual('{0!s:15s}'.format('Hello'), 'Hello ')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
757 self.assertEqual('{0!r}'.format('Hello'), "'Hello'")
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
758 self.assertEqual('{0!r:}'.format('Hello'), "'Hello'")
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
759 self.assertEqual('{0!r}'.format(F('Hello')), 'F(Hello)')
49142
51ff934562c2 The Unicode database was updated to 5.1, and some characters have become printable.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49032
diff changeset
760 self.assertEqual('{0!r}'.format('\u0378'), "'\\u0378'") # nonprintable
48427
4e6859c6d38c Issue #3282: str.isprintable() should return False for undefined Unicode characters.
Georg Brandl <georg@python.org>
parents: 47611
diff changeset
761 self.assertEqual('{0!r}'.format('\u0374'), "'\u0374'") # printable
4e6859c6d38c Issue #3282: str.isprintable() should return False for undefined Unicode characters.
Georg Brandl <georg@python.org>
parents: 47611
diff changeset
762 self.assertEqual('{0!r}'.format(F('\u0374')), 'F(\u0374)')
47611
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
763 self.assertEqual('{0!a}'.format('Hello'), "'Hello'")
49142
51ff934562c2 The Unicode database was updated to 5.1, and some characters have become printable.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49032
diff changeset
764 self.assertEqual('{0!a}'.format('\u0378'), "'\\u0378'") # nonprintable
48427
4e6859c6d38c Issue #3282: str.isprintable() should return False for undefined Unicode characters.
Georg Brandl <georg@python.org>
parents: 47611
diff changeset
765 self.assertEqual('{0!a}'.format('\u0374'), "'\\u0374'") # printable
47611
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
766 self.assertEqual('{0!a:}'.format('Hello'), "'Hello'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
767 self.assertEqual('{0!a}'.format(F('Hello')), 'F(Hello)')
48427
4e6859c6d38c Issue #3282: str.isprintable() should return False for undefined Unicode characters.
Georg Brandl <georg@python.org>
parents: 47611
diff changeset
768 self.assertEqual('{0!a}'.format(F('\u0374')), 'F(\\u0374)')
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
769
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
770 # test fallback to object.__format__
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
771 self.assertEqual('{0}'.format({}), '{}')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
772 self.assertEqual('{0}'.format([]), '[]')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
773 self.assertEqual('{0}'.format([1]), '[1]')
64724
9f55faaa3193 Issue 7994: Make object.__format__() raise a PendingDeprecationWarning
Eric Smith <eric@trueblade.com>
parents: 64694
diff changeset
774
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
775 self.assertEqual('{0:d}'.format(G('data')), 'G(data)')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
776 self.assertEqual('{0!s}'.format(G('data')), 'string is data')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
777
64724
9f55faaa3193 Issue 7994: Make object.__format__() raise a PendingDeprecationWarning
Eric Smith <eric@trueblade.com>
parents: 64694
diff changeset
778 msg = 'object.__format__ with a non-empty format string is deprecated'
9f55faaa3193 Issue 7994: Make object.__format__() raise a PendingDeprecationWarning
Eric Smith <eric@trueblade.com>
parents: 64694
diff changeset
779 with support.check_warnings((msg, PendingDeprecationWarning)):
9f55faaa3193 Issue 7994: Make object.__format__() raise a PendingDeprecationWarning
Eric Smith <eric@trueblade.com>
parents: 64694
diff changeset
780 self.assertEqual('{0:^10}'.format(E('data')), ' E(data) ')
9f55faaa3193 Issue 7994: Make object.__format__() raise a PendingDeprecationWarning
Eric Smith <eric@trueblade.com>
parents: 64694
diff changeset
781 self.assertEqual('{0:^10s}'.format(E('data')), ' E(data) ')
9f55faaa3193 Issue 7994: Make object.__format__() raise a PendingDeprecationWarning
Eric Smith <eric@trueblade.com>
parents: 64694
diff changeset
782 self.assertEqual('{0:>15s}'.format(G('data')), ' string is data')
9f55faaa3193 Issue 7994: Make object.__format__() raise a PendingDeprecationWarning
Eric Smith <eric@trueblade.com>
parents: 64694
diff changeset
783
42909
7a62c2469dea Additional test for formatting code.
Eric Smith <eric@trueblade.com>
parents: 42907
diff changeset
784 self.assertEqual("{0:date: %Y-%m-%d}".format(I(year=2007,
7a62c2469dea Additional test for formatting code.
Eric Smith <eric@trueblade.com>
parents: 42907
diff changeset
785 month=8,
7a62c2469dea Additional test for formatting code.
Eric Smith <eric@trueblade.com>
parents: 42907
diff changeset
786 day=27)),
7a62c2469dea Additional test for formatting code.
Eric Smith <eric@trueblade.com>
parents: 42907
diff changeset
787 "date: 2007-08-27")
7a62c2469dea Additional test for formatting code.
Eric Smith <eric@trueblade.com>
parents: 42907
diff changeset
788
43097
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42909
diff changeset
789 # test deriving from a builtin type and overriding __format__
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42909
diff changeset
790 self.assertEqual("{0}".format(J(10)), "20")
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42909
diff changeset
791
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42909
diff changeset
792
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
793 # string format specifiers
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
794 self.assertEqual('{0:}'.format('a'), 'a')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
795
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
796 # computed format specifiers
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
797 self.assertEqual("{0:.{1}}".format('hello world', 5), 'hello')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
798 self.assertEqual("{0:.{1}s}".format('hello world', 5), 'hello')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
799 self.assertEqual("{0:.{precision}s}".format('hello world', precision=5), 'hello')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
800 self.assertEqual("{0:{width}.{precision}s}".format('hello world', width=10, precision=5), 'hello ')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
801 self.assertEqual("{0:{width}.{precision}s}".format('hello world', width='10', precision='5'), 'hello ')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
802
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
803 # test various errors
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
804 self.assertRaises(ValueError, '{'.format)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
805 self.assertRaises(ValueError, '}'.format)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
806 self.assertRaises(ValueError, 'a{'.format)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
807 self.assertRaises(ValueError, 'a}'.format)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
808 self.assertRaises(ValueError, '{a'.format)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
809 self.assertRaises(ValueError, '}a'.format)
43241
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
810 self.assertRaises(IndexError, '{0}'.format)
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
811 self.assertRaises(IndexError, '{1}'.format, 'abc')
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
812 self.assertRaises(KeyError, '{x}'.format)
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
813 self.assertRaises(ValueError, "}{".format)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
814 self.assertRaises(ValueError, "abc{0:{}".format)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
815 self.assertRaises(ValueError, "{0".format)
43241
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
816 self.assertRaises(IndexError, "{0.}".format)
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
817 self.assertRaises(ValueError, "{0.}".format, 0)
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
818 self.assertRaises(IndexError, "{0[}".format)
43206
f4fb74f63806 Fix segfault discovered by Ron Adam. Not checking for terminating right bracket in "'{0[}'.format(())". Fixed, and tests added.
Eric Smith <eric@trueblade.com>
parents: 43172
diff changeset
819 self.assertRaises(ValueError, "{0[}".format, [])
43241
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
820 self.assertRaises(KeyError, "{0]}".format)
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
821 self.assertRaises(ValueError, "{0.[]}".format, 0)
42875
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
822 self.assertRaises(ValueError, "{0..foo}".format, 0)
43241
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
823 self.assertRaises(ValueError, "{0[0}".format, 0)
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
824 self.assertRaises(ValueError, "{0[0:foo}".format, 0)
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
825 self.assertRaises(KeyError, "{c]}".format)
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
826 self.assertRaises(ValueError, "{{ {{{0}}".format, 0)
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
827 self.assertRaises(ValueError, "{0}}".format, 0)
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
828 self.assertRaises(KeyError, "{foo}".format, bar=3)
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
829 self.assertRaises(ValueError, "{0!x}".format, 3)
43241
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
830 self.assertRaises(ValueError, "{0!}".format, 0)
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
831 self.assertRaises(ValueError, "{0!rs}".format, 0)
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
832 self.assertRaises(ValueError, "{!}".format)
52159
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
833 self.assertRaises(IndexError, "{:}".format)
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
834 self.assertRaises(IndexError, "{:s}".format)
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
835 self.assertRaises(IndexError, "{}".format)
62185
b5af5c93135b Merged revisions 81823,81835 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59899
diff changeset
836 big = "23098475029384702983476098230754973209482573"
b5af5c93135b Merged revisions 81823,81835 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59899
diff changeset
837 self.assertRaises(ValueError, ("{" + big + "}").format)
b5af5c93135b Merged revisions 81823,81835 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59899
diff changeset
838 self.assertRaises(ValueError, ("{[" + big + "]}").format, [0])
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
839
54269
2bcac08cfc6d Merged revisions 72848 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 53965
diff changeset
840 # issue 6089
2bcac08cfc6d Merged revisions 72848 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 53965
diff changeset
841 self.assertRaises(ValueError, "{0[0]x}".format, [None])
2bcac08cfc6d Merged revisions 72848 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 53965
diff changeset
842 self.assertRaises(ValueError, "{0[0](10)}".format, [None])
2bcac08cfc6d Merged revisions 72848 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 53965
diff changeset
843
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
844 # can't have a replacement on the field name portion
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
845 self.assertRaises(TypeError, '{0[{1}]}'.format, 'abcdefg', 4)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
846
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
847 # exceed maximum recursion depth
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
848 self.assertRaises(ValueError, "{0:{1:{2}}}".format, 'abc', 's', '')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
849 self.assertRaises(ValueError, "{0:{1:{2:{3:{4:{5:{6}}}}}}}".format,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
850 0, 1, 2, 3, 4, 5, 6, 7)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
851
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
852 # string format spec errors
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
853 self.assertRaises(ValueError, "{0:-s}".format, '')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
854 self.assertRaises(ValueError, format, "", "-")
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
855 self.assertRaises(ValueError, "{0:=s}".format, '')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
856
48472
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48430
diff changeset
857 # Alternate formatting is not supported
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48430
diff changeset
858 self.assertRaises(ValueError, format, '', '#')
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48430
diff changeset
859 self.assertRaises(ValueError, format, '', '#20')
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48430
diff changeset
860
65940
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
861 def test_format_map(self):
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
862 self.assertEqual(''.format_map({}), '')
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
863 self.assertEqual('a'.format_map({}), 'a')
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
864 self.assertEqual('ab'.format_map({}), 'ab')
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
865 self.assertEqual('a{{'.format_map({}), 'a{')
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
866 self.assertEqual('a}}'.format_map({}), 'a}')
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
867 self.assertEqual('{{b'.format_map({}), '{b')
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
868 self.assertEqual('}}b'.format_map({}), '}b')
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
869 self.assertEqual('a{{b'.format_map({}), 'a{b')
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
870
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
871 # using mappings
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
872 class Mapping(dict):
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
873 def __missing__(self, key):
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
874 return key
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
875 self.assertEqual('{hello}'.format_map(Mapping()), 'hello')
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
876 self.assertEqual('{a} {world}'.format_map(Mapping(a='hello')), 'hello world')
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
877
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
878 class InternalMapping:
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
879 def __init__(self):
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
880 self.mapping = {'a': 'hello'}
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
881 def __getitem__(self, key):
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
882 return self.mapping[key]
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
883 self.assertEqual('{a}'.format_map(InternalMapping()), 'hello')
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
884
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
885
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
886 class C:
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
887 def __init__(self, x=100):
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
888 self._x = x
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
889 def __format__(self, spec):
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
890 return spec
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
891 self.assertEqual('{foo._x}'.format_map({'foo': C(20)}), '20')
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
892
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
893 # test various errors
75558
995cddd15313 str.format_map tests don't do what they say: fix to actually implement the intent of the test. Closes #13450.
Eric V. Smith <eric@trueblade.com>
parents: 73680
diff changeset
894 self.assertRaises(TypeError, ''.format_map)
995cddd15313 str.format_map tests don't do what they say: fix to actually implement the intent of the test. Closes #13450.
Eric V. Smith <eric@trueblade.com>
parents: 73680
diff changeset
895 self.assertRaises(TypeError, 'a'.format_map)
995cddd15313 str.format_map tests don't do what they say: fix to actually implement the intent of the test. Closes #13450.
Eric V. Smith <eric@trueblade.com>
parents: 73680
diff changeset
896
995cddd15313 str.format_map tests don't do what they say: fix to actually implement the intent of the test. Closes #13450.
Eric V. Smith <eric@trueblade.com>
parents: 73680
diff changeset
897 self.assertRaises(ValueError, '{'.format_map, {})
995cddd15313 str.format_map tests don't do what they say: fix to actually implement the intent of the test. Closes #13450.
Eric V. Smith <eric@trueblade.com>
parents: 73680
diff changeset
898 self.assertRaises(ValueError, '}'.format_map, {})
995cddd15313 str.format_map tests don't do what they say: fix to actually implement the intent of the test. Closes #13450.
Eric V. Smith <eric@trueblade.com>
parents: 73680
diff changeset
899 self.assertRaises(ValueError, 'a{'.format_map, {})
995cddd15313 str.format_map tests don't do what they say: fix to actually implement the intent of the test. Closes #13450.
Eric V. Smith <eric@trueblade.com>
parents: 73680
diff changeset
900 self.assertRaises(ValueError, 'a}'.format_map, {})
995cddd15313 str.format_map tests don't do what they say: fix to actually implement the intent of the test. Closes #13450.
Eric V. Smith <eric@trueblade.com>
parents: 73680
diff changeset
901 self.assertRaises(ValueError, '{a'.format_map, {})
995cddd15313 str.format_map tests don't do what they say: fix to actually implement the intent of the test. Closes #13450.
Eric V. Smith <eric@trueblade.com>
parents: 73680
diff changeset
902 self.assertRaises(ValueError, '}a'.format_map, {})
65940
05eed9f0ce01 Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Eric Smith <eric@trueblade.com>
parents: 65660
diff changeset
903
71409
f6d074a7bbd4 Closes #12579. Positional fields with str.format_map() now raise a ValueError instead of SystemError.
Eric V. Smith <eric@trueblade.com>
parents: 71181
diff changeset
904 # issue #12579: can't supply positional params to format_map
f6d074a7bbd4 Closes #12579. Positional fields with str.format_map() now raise a ValueError instead of SystemError.
Eric V. Smith <eric@trueblade.com>
parents: 71181
diff changeset
905 self.assertRaises(ValueError, '{}'.format_map, {'a' : 2})
f6d074a7bbd4 Closes #12579. Positional fields with str.format_map() now raise a ValueError instead of SystemError.
Eric V. Smith <eric@trueblade.com>
parents: 71181
diff changeset
906 self.assertRaises(ValueError, '{}'.format_map, 'a')
f6d074a7bbd4 Closes #12579. Positional fields with str.format_map() now raise a ValueError instead of SystemError.
Eric V. Smith <eric@trueblade.com>
parents: 71181
diff changeset
907 self.assertRaises(ValueError, '{a} {}'.format_map, {"a" : 2, "b" : 1})
f6d074a7bbd4 Closes #12579. Positional fields with str.format_map() now raise a ValueError instead of SystemError.
Eric V. Smith <eric@trueblade.com>
parents: 71181
diff changeset
908
52159
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
909 def test_format_auto_numbering(self):
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
910 class C:
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
911 def __init__(self, x=100):
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
912 self._x = x
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
913 def __format__(self, spec):
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
914 return spec
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
915
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
916 self.assertEqual('{}'.format(10), '10')
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
917 self.assertEqual('{:5}'.format('s'), 's ')
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
918 self.assertEqual('{!r}'.format('s'), "'s'")
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
919 self.assertEqual('{._x}'.format(C(10)), '10')
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
920 self.assertEqual('{[1]}'.format([1, 2]), '2')
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
921 self.assertEqual('{[a]}'.format({'a':4, 'b':2}), '4')
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
922 self.assertEqual('a{}b{}c'.format(0, 1), 'a0b1c')
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
923
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
924 self.assertEqual('a{:{}}b'.format('x', '^10'), 'a x b')
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
925 self.assertEqual('a{:{}x}b'.format(20, '#'), 'a0x14b')
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
926
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
927 # can't mix and match numbering and auto-numbering
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
928 self.assertRaises(ValueError, '{}{1}'.format, 1, 2)
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
929 self.assertRaises(ValueError, '{1}{}'.format, 1, 2)
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
930 self.assertRaises(ValueError, '{:{1}}'.format, 1, 2)
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
931 self.assertRaises(ValueError, '{0:{}}'.format, 1, 2)
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
932
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
933 # can mix and match auto-numbering and named
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
934 self.assertEqual('{f}{}'.format(4, f='test'), 'test4')
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
935 self.assertEqual('{}{f}'.format(4, f='test'), '4test')
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
936 self.assertEqual('{:{f}}{g}{}'.format(1, 3, g='g', f=2), ' 1g3')
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
937 self.assertEqual('{f:{}}{}{g}'.format(2, 4, f=1, g='g'), ' 14g')
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
938
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
939 def test_formatting(self):
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
940 string_tests.MixinStrUnicodeUserStringTest.test_formatting(self)
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
941 # Testing Unicode formatting strings...
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
942 self.assertEqual("%s, %s" % ("abc", "abc"), 'abc, abc')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
943 self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", 1, 2, 3), 'abc, abc, 1, 2.000000, 3.00')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
944 self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", 1, -2, 3), 'abc, abc, 1, -2.000000, 3.00')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
945 self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", -1, -2, 3.5), 'abc, abc, -1, -2.000000, 3.50')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
946 self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", -1, -2, 3.57), 'abc, abc, -1, -2.000000, 3.57')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
947 self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", -1, -2, 1003.57), 'abc, abc, -1, -2.000000, 1003.57')
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
948 if not sys.platform.startswith('java'):
41620
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
949 self.assertEqual("%r, %r" % (b"abc", "abc"), "b'abc', 'abc'")
47611
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
950 self.assertEqual("%r" % ("\u1234",), "'\u1234'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
951 self.assertEqual("%a" % ("\u1234",), "'\\u1234'")
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
952 self.assertEqual("%(x)s, %(y)s" % {'x':"abc", 'y':"def"}, 'abc, def')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
953 self.assertEqual("%(x)s, %(\xfc)s" % {'x':"abc", '\xfc':"def"}, 'abc, def')
28479
e13a7e1fdb9e Remove duplicate test.
Walter Dörwald <walter@livinglogic.de>
parents: 28478
diff changeset
954
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
955 self.assertEqual('%c' % 0x1234, '\u1234')
48430
04817793b962 Issue #3280: like chr() already does, the "%c" format now accepts the full unicode range
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48427
diff changeset
956 self.assertEqual('%c' % 0x21483, '\U00021483')
04817793b962 Issue #3280: like chr() already does, the "%c" format now accepts the full unicode range
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48427
diff changeset
957 self.assertRaises(OverflowError, "%c".__mod__, (0x110000,))
04817793b962 Issue #3280: like chr() already does, the "%c" format now accepts the full unicode range
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48427
diff changeset
958 self.assertEqual('%c' % '\U00021483', '\U00021483')
04817793b962 Issue #3280: like chr() already does, the "%c" format now accepts the full unicode range
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48427
diff changeset
959 self.assertRaises(TypeError, "%c".__mod__, "aa")
63101
5cff058d6ced Merged revisions 82978 via svnmerge from
Stefan Krah <stefan@bytereef.org>
parents: 62710
diff changeset
960 self.assertRaises(ValueError, "%.1\u1032f".__mod__, (1.0/3))
71181
97707459bb5a Fix closes issue12471 - wrong TypeError message when '%i' format spec was used.
Senthil Kumaran <senthil@uthcode.com>
parents: 69564
diff changeset
961 self.assertRaises(TypeError, "%i".__mod__, "aa")
20563
9058e49c6c57 Additional test and documentation for the unicode() changes.
Marc-André Lemburg <mal@egenix.com>
parents: 20240
diff changeset
962
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
963 # formatting jobs delegated from the string implementation:
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
964 self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
965 self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
966 self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...')
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
967 self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...')
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
968 self.assertEqual('...%(foo)s...' % {'foo':"abc",'def':123}, '...abc...')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
969 self.assertEqual('...%(foo)s...' % {'foo':"abc",'def':123}, '...abc...')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
970 self.assertEqual('...%s...%s...%s...%s...' % (1,2,3,"abc"), '...1...2...3...abc...')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
971 self.assertEqual('...%%...%%s...%s...%s...%s...%s...' % (1,2,3,"abc"), '...%...%s...1...2...3...abc...')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
972 self.assertEqual('...%s...' % "abc", '...abc...')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
973 self.assertEqual('%*s' % (5,'abc',), ' abc')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
974 self.assertEqual('%*s' % (-5,'abc',), 'abc ')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
975 self.assertEqual('%*.*s' % (5,2,'abc',), ' ab')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
976 self.assertEqual('%*.*s' % (5,3,'abc',), ' abc')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
977 self.assertEqual('%i %*.*s' % (10, 5,3,'abc',), '10 abc')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
978 self.assertEqual('%i%s %*.*s' % (10, 3, 5, 3, 'abc',), '103 abc')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
979 self.assertEqual('%c' % 'a', 'a')
35331
ef11cbc08dab Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 34931
diff changeset
980 class Wrapper:
ef11cbc08dab Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 34931
diff changeset
981 def __str__(self):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
982 return '\u1234'
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
983 self.assertEqual('%s' % Wrapper(), '\u1234')
20563
9058e49c6c57 Additional test and documentation for the unicode() changes.
Marc-André Lemburg <mal@egenix.com>
parents: 20240
diff changeset
984
53849
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53742
diff changeset
985 # issue 3382
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53742
diff changeset
986 NAN = float('nan')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53742
diff changeset
987 INF = float('inf')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53742
diff changeset
988 self.assertEqual('%f' % NAN, 'nan')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53742
diff changeset
989 self.assertEqual('%F' % NAN, 'NAN')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53742
diff changeset
990 self.assertEqual('%f' % INF, 'inf')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53742
diff changeset
991 self.assertEqual('%F' % INF, 'INF')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53742
diff changeset
992
69563
bcbf8c3c4a88 #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66340
diff changeset
993 def test_startswith_endswith_errors(self):
bcbf8c3c4a88 #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66340
diff changeset
994 for meth in ('foo'.startswith, 'foo'.endswith):
69564
f393c507717a #6780: merge with 3.1.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68257 69563
diff changeset
995 with self.assertRaises(TypeError) as cm:
69563
bcbf8c3c4a88 #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66340
diff changeset
996 meth(['f'])
69564
f393c507717a #6780: merge with 3.1.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68257 69563
diff changeset
997 exc = str(cm.exception)
69563
bcbf8c3c4a88 #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66340
diff changeset
998 self.assertIn('str', exc)
bcbf8c3c4a88 #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66340
diff changeset
999 self.assertIn('tuple', exc)
bcbf8c3c4a88 #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66340
diff changeset
1000
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46307
diff changeset
1001 @support.run_with_locale('LC_ALL', 'de_DE', 'fr_FR')
36341
4bcf5fe54d83 Checkin the test of patch #1400181.
Georg Brandl <georg@python.org>
parents: 36056
diff changeset
1002 def test_format_float(self):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1003 # should not format with a comma, but always with C locale
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1004 self.assertEqual('1.0', '%.1f' % 1.0)
36341
4bcf5fe54d83 Checkin the test of patch #1400181.
Georg Brandl <georg@python.org>
parents: 36056
diff changeset
1005
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1006 def test_constructor(self):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1007 # unicode(obj) tests (this maps to PyObject_Unicode() at C level)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1008
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1009 self.assertEqual(
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1010 str('unicode remains unicode'),
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1011 'unicode remains unicode'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1012 )
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1013
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1014 class UnicodeSubclass(str):
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1015 pass
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 18828
diff changeset
1016
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1017 self.assertEqual(
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1018 str(UnicodeSubclass('unicode subclass becomes unicode')),
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1019 'unicode subclass becomes unicode'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1020 )
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1021
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1022 self.assertEqual(
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1023 str('strings are converted to unicode'),
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1024 'strings are converted to unicode'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1025 )
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 18828
diff changeset
1026
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1027 class StringCompat:
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1028 def __init__(self, x):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1029 self.x = x
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1030 def __str__(self):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1031 return self.x
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1032
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1033 self.assertEqual(
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1034 str(StringCompat('__str__ compatible objects are recognized')),
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1035 '__str__ compatible objects are recognized'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1036 )
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1037
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1038 # unicode(obj) is compatible to str():
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1039
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1040 o = StringCompat('unicode(obj) is compatible to str()')
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1041 self.assertEqual(str(o), 'unicode(obj) is compatible to str()')
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1042 self.assertEqual(str(o), 'unicode(obj) is compatible to str()')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1043
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1044 for obj in (123, 123.45, 123):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1045 self.assertEqual(str(obj), str(str(obj)))
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1046
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1047 # unicode(obj, encoding, error) tests (this maps to
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1048 # PyUnicode_FromEncodedObject() at C level)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1049
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1050 if not sys.platform.startswith('java'):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1051 self.assertRaises(
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1052 TypeError,
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1053 str,
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1054 'decoding unicode is not supported',
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1055 'utf-8',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1056 'strict'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1057 )
21882
94bac88f150f Fix for the UTF-8 memory allocation bug and the UTF-8 encoding
Marc-André Lemburg <mal@egenix.com>
parents: 21337
diff changeset
1058
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1059 self.assertEqual(
41620
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1060 str(b'strings are decoded to unicode', 'utf-8', 'strict'),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1061 'strings are decoded to unicode'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1062 )
13410
5157eb7be20d Tests for new surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13400
diff changeset
1063
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1064 if not sys.platform.startswith('java'):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1065 self.assertEqual(
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1066 str(
43487
8c10aac37037 Breaking ground for PEP 3137 implementation:
Guido van Rossum <guido@python.org>
parents: 43446
diff changeset
1067 memoryview(b'character buffers are decoded to unicode'),
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1068 'utf-8',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1069 'strict'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1070 ),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1071 'character buffers are decoded to unicode'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1072 )
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1073
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1074 self.assertRaises(TypeError, str, 42, 42, 42)
13410
5157eb7be20d Tests for new surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13400
diff changeset
1075
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1076 def test_codecs_utf7(self):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1077 utfTests = [
41620
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1078 ('A\u2262\u0391.', b'A+ImIDkQ.'), # RFC2152 example
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1079 ('Hi Mom -\u263a-!', b'Hi Mom -+Jjo--!'), # RFC2152 example
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1080 ('\u65E5\u672C\u8A9E', b'+ZeVnLIqe-'), # RFC2152 example
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1081 ('Item 3 is \u00a31.', b'Item 3 is +AKM-1.'), # RFC2152 example
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1082 ('+', b'+-'),
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1083 ('+-', b'+--'),
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1084 ('+?', b'+-?'),
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1085 ('\?', b'+AFw?'),
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1086 ('+?', b'+-?'),
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1087 (r'\\?', b'+AFwAXA?'),
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1088 (r'\\\?', b'+AFwAXABc?'),
53742
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1089 (r'++--', b'+-+---'),
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1090 ('\U000abcde', b'+2m/c3g-'), # surrogate pairs
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1091 ('/', b'/'),
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1092 ]
12051
d89556c04ef9 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
1093
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1094 for (x, y) in utfTests:
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1095 self.assertEqual(x.encode('utf-7'), y)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1096
73552
ddfcb0de564f Issue #13333: The UTF-7 decoder now accepts lone surrogates
Antoine Pitrou <solipsis@pitrou.net>
parents: 72813
diff changeset
1097 # Unpaired surrogates are passed through
ddfcb0de564f Issue #13333: The UTF-7 decoder now accepts lone surrogates
Antoine Pitrou <solipsis@pitrou.net>
parents: 72813
diff changeset
1098 self.assertEqual('\uD801'.encode('utf-7'), b'+2AE-')
ddfcb0de564f Issue #13333: The UTF-7 decoder now accepts lone surrogates
Antoine Pitrou <solipsis@pitrou.net>
parents: 72813
diff changeset
1099 self.assertEqual('\uD801x'.encode('utf-7'), b'+2AE-x')
ddfcb0de564f Issue #13333: The UTF-7 decoder now accepts lone surrogates
Antoine Pitrou <solipsis@pitrou.net>
parents: 72813
diff changeset
1100 self.assertEqual('\uDC01'.encode('utf-7'), b'+3AE-')
ddfcb0de564f Issue #13333: The UTF-7 decoder now accepts lone surrogates
Antoine Pitrou <solipsis@pitrou.net>
parents: 72813
diff changeset
1101 self.assertEqual('\uDC01x'.encode('utf-7'), b'+3AE-x')
ddfcb0de564f Issue #13333: The UTF-7 decoder now accepts lone surrogates
Antoine Pitrou <solipsis@pitrou.net>
parents: 72813
diff changeset
1102 self.assertEqual(b'+2AE-'.decode('utf-7'), '\uD801')
ddfcb0de564f Issue #13333: The UTF-7 decoder now accepts lone surrogates
Antoine Pitrou <solipsis@pitrou.net>
parents: 72813
diff changeset
1103 self.assertEqual(b'+2AE-x'.decode('utf-7'), '\uD801x')
ddfcb0de564f Issue #13333: The UTF-7 decoder now accepts lone surrogates
Antoine Pitrou <solipsis@pitrou.net>
parents: 72813
diff changeset
1104 self.assertEqual(b'+3AE-'.decode('utf-7'), '\uDC01')
ddfcb0de564f Issue #13333: The UTF-7 decoder now accepts lone surrogates
Antoine Pitrou <solipsis@pitrou.net>
parents: 72813
diff changeset
1105 self.assertEqual(b'+3AE-x'.decode('utf-7'), '\uDC01x')
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1106
73552
ddfcb0de564f Issue #13333: The UTF-7 decoder now accepts lone surrogates
Antoine Pitrou <solipsis@pitrou.net>
parents: 72813
diff changeset
1107 self.assertEqual('\uD801\U000abcde'.encode('utf-7'), b'+2AHab9ze-')
ddfcb0de564f Issue #13333: The UTF-7 decoder now accepts lone surrogates
Antoine Pitrou <solipsis@pitrou.net>
parents: 72813
diff changeset
1108 self.assertEqual(b'+2AHab9ze-'.decode('utf-7'), '\uD801\U000abcde')
12331
d195f17f2927 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
1109
48570
862c0eab1c50 Merged revisions 65227 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48472
diff changeset
1110 # Issue #2242: crash on some Windows/MSVC versions
53742
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1111 self.assertEqual(b'+\xc1'.decode('utf-7'), '\xc1')
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1112
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1113 # Direct encoded characters
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1114 set_d = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'(),-./:?"
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1115 # Optional direct characters
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1116 set_o = '!"#$%&*;<=>@[]^_`{|}'
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1117 for c in set_d:
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1118 self.assertEqual(c.encode('utf7'), c.encode('ascii'))
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1119 self.assertEqual(c.encode('ascii').decode('utf7'), c)
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1120 for c in set_o:
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1121 self.assertEqual(c.encode('ascii').decode('utf7'), c)
48570
862c0eab1c50 Merged revisions 65227 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48472
diff changeset
1122
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1123 def test_codecs_utf8(self):
41620
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1124 self.assertEqual(''.encode('utf-8'), b'')
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1125 self.assertEqual('\u20ac'.encode('utf-8'), b'\xe2\x82\xac')
54484
6846f95f8e07 Issue #6150: Fix test_unicode on wide-unicode builds.
Martin v. Löwis <martin@v.loewis.de>
parents: 54269
diff changeset
1126 if sys.maxunicode == 65535:
6846f95f8e07 Issue #6150: Fix test_unicode on wide-unicode builds.
Martin v. Löwis <martin@v.loewis.de>
parents: 54269
diff changeset
1127 self.assertEqual('\ud800\udc02'.encode('utf-8'), b'\xf0\x90\x80\x82')
6846f95f8e07 Issue #6150: Fix test_unicode on wide-unicode builds.
Martin v. Löwis <martin@v.loewis.de>
parents: 54269
diff changeset
1128 self.assertEqual('\ud84d\udc56'.encode('utf-8'), b'\xf0\xa3\x91\x96')
53965
fbcdae3e0ca1 Rename the surrogates error handler to surrogatepass.
Martin v. Löwis <martin@v.loewis.de>
parents: 53849
diff changeset
1129 self.assertEqual('\ud800'.encode('utf-8', 'surrogatepass'), b'\xed\xa0\x80')
fbcdae3e0ca1 Rename the surrogates error handler to surrogatepass.
Martin v. Löwis <martin@v.loewis.de>
parents: 53849
diff changeset
1130 self.assertEqual('\udc00'.encode('utf-8', 'surrogatepass'), b'\xed\xb0\x80')
54484
6846f95f8e07 Issue #6150: Fix test_unicode on wide-unicode builds.
Martin v. Löwis <martin@v.loewis.de>
parents: 54269
diff changeset
1131 if sys.maxunicode == 65535:
6846f95f8e07 Issue #6150: Fix test_unicode on wide-unicode builds.
Martin v. Löwis <martin@v.loewis.de>
parents: 54269
diff changeset
1132 self.assertEqual(
6846f95f8e07 Issue #6150: Fix test_unicode on wide-unicode builds.
Martin v. Löwis <martin@v.loewis.de>
parents: 54269
diff changeset
1133 ('\ud800\udc02'*1000).encode('utf-8'),
6846f95f8e07 Issue #6150: Fix test_unicode on wide-unicode builds.
Martin v. Löwis <martin@v.loewis.de>
parents: 54269
diff changeset
1134 b'\xf0\x90\x80\x82'*1000)
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1135 self.assertEqual(
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1136 '\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f'
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1137 '\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00'
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1138 '\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c'
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1139 '\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067'
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1140 '\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das'
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1141 ' Nunstuck git und'.encode('utf-8'),
41620
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1142 b'\xe6\xad\xa3\xe7\xa2\xba\xe3\x81\xab\xe8\xa8\x80\xe3\x81'
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1143 b'\x86\xe3\x81\xa8\xe7\xbf\xbb\xe8\xa8\xb3\xe3\x81\xaf\xe3'
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1144 b'\x81\x95\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x84\xe3\x81\xbe'
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1145 b'\xe3\x81\x9b\xe3\x82\x93\xe3\x80\x82\xe4\xb8\x80\xe9\x83'
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1146 b'\xa8\xe3\x81\xaf\xe3\x83\x89\xe3\x82\xa4\xe3\x83\x84\xe8'
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1147 b'\xaa\x9e\xe3\x81\xa7\xe3\x81\x99\xe3\x81\x8c\xe3\x80\x81'
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1148 b'\xe3\x81\x82\xe3\x81\xa8\xe3\x81\xaf\xe3\x81\xa7\xe3\x81'
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1149 b'\x9f\xe3\x82\x89\xe3\x82\x81\xe3\x81\xa7\xe3\x81\x99\xe3'
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1150 b'\x80\x82\xe5\xae\x9f\xe9\x9a\x9b\xe3\x81\xab\xe3\x81\xaf'
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1151 b'\xe3\x80\x8cWenn ist das Nunstuck git und'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1152 )
22423
b523a470a0ce Do not insert characters for unicode-escape decoders if the error mode
Martin v. Löwis <martin@v.loewis.de>
parents: 21886
diff changeset
1153
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1154 # UTF-8 specific decoding tests
41620
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1155 self.assertEqual(str(b'\xf0\xa3\x91\x96', 'utf-8'), '\U00023456' )
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1156 self.assertEqual(str(b'\xf0\x90\x80\x82', 'utf-8'), '\U00010002' )
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1157 self.assertEqual(str(b'\xe2\x82\xac', 'utf-8'), '\u20ac' )
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1158
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1159 # Other possible utf-8 test cases:
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1160 # * strict decoding testing for all of the
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1161 # UTF8_ERROR cases in PyUnicode_DecodeUTF8
12051
d89556c04ef9 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
1162
62710
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1163 def test_utf8_decode_valid_sequences(self):
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1164 sequences = [
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1165 # single byte
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1166 (b'\x00', '\x00'), (b'a', 'a'), (b'\x7f', '\x7f'),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1167 # 2 bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1168 (b'\xc2\x80', '\x80'), (b'\xdf\xbf', '\u07ff'),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1169 # 3 bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1170 (b'\xe0\xa0\x80', '\u0800'), (b'\xed\x9f\xbf', '\ud7ff'),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1171 (b'\xee\x80\x80', '\uE000'), (b'\xef\xbf\xbf', '\uffff'),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1172 # 4 bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1173 (b'\xF0\x90\x80\x80', '\U00010000'),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1174 (b'\xf4\x8f\xbf\xbf', '\U0010FFFF')
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1175 ]
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1176 for seq, res in sequences:
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1177 self.assertEqual(seq.decode('utf-8'), res)
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1178
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1179
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1180 def test_utf8_decode_invalid_sequences(self):
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1181 # continuation bytes in a sequence of 2, 3, or 4 bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1182 continuation_bytes = [bytes([x]) for x in range(0x80, 0xC0)]
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1183 # start bytes of a 2-byte sequence equivalent to codepoints < 0x7F
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1184 invalid_2B_seq_start_bytes = [bytes([x]) for x in range(0xC0, 0xC2)]
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1185 # start bytes of a 4-byte sequence equivalent to codepoints > 0x10FFFF
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1186 invalid_4B_seq_start_bytes = [bytes([x]) for x in range(0xF5, 0xF8)]
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1187 invalid_start_bytes = (
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1188 continuation_bytes + invalid_2B_seq_start_bytes +
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1189 invalid_4B_seq_start_bytes + [bytes([x]) for x in range(0xF7, 0x100)]
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1190 )
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1191
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1192 for byte in invalid_start_bytes:
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1193 self.assertRaises(UnicodeDecodeError, byte.decode, 'utf-8')
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1194
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1195 for sb in invalid_2B_seq_start_bytes:
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1196 for cb in continuation_bytes:
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1197 self.assertRaises(UnicodeDecodeError, (sb+cb).decode, 'utf-8')
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1198
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1199 for sb in invalid_4B_seq_start_bytes:
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1200 for cb1 in continuation_bytes[:3]:
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1201 for cb3 in continuation_bytes[:3]:
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1202 self.assertRaises(UnicodeDecodeError,
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1203 (sb+cb1+b'\x80'+cb3).decode, 'utf-8')
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1204
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1205 for cb in [bytes([x]) for x in range(0x80, 0xA0)]:
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1206 self.assertRaises(UnicodeDecodeError,
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1207 (b'\xE0'+cb+b'\x80').decode, 'utf-8')
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1208 self.assertRaises(UnicodeDecodeError,
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1209 (b'\xE0'+cb+b'\xBF').decode, 'utf-8')
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1210 # surrogates
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1211 for cb in [bytes([x]) for x in range(0xA0, 0xC0)]:
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1212 self.assertRaises(UnicodeDecodeError,
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1213 (b'\xED'+cb+b'\x80').decode, 'utf-8')
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1214 self.assertRaises(UnicodeDecodeError,
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1215 (b'\xED'+cb+b'\xBF').decode, 'utf-8')
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1216 for cb in [bytes([x]) for x in range(0x80, 0x90)]:
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1217 self.assertRaises(UnicodeDecodeError,
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1218 (b'\xF0'+cb+b'\x80\x80').decode, 'utf-8')
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1219 self.assertRaises(UnicodeDecodeError,
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1220 (b'\xF0'+cb+b'\xBF\xBF').decode, 'utf-8')
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1221 for cb in [bytes([x]) for x in range(0x90, 0xC0)]:
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1222 self.assertRaises(UnicodeDecodeError,
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1223 (b'\xF4'+cb+b'\x80\x80').decode, 'utf-8')
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1224 self.assertRaises(UnicodeDecodeError,
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1225 (b'\xF4'+cb+b'\xBF\xBF').decode, 'utf-8')
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1226
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1227 def test_issue8271(self):
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1228 # Issue #8271: during the decoding of an invalid UTF-8 byte sequence,
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1229 # only the start byte and the continuation byte(s) are now considered
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1230 # invalid, instead of the number of bytes specified by the start byte.
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1231 # See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf (page 95,
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1232 # table 3-8, Row 2) for more information about the algorithm used.
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1233 FFFD = '\ufffd'
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1234 sequences = [
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1235 # invalid start bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1236 (b'\x80', FFFD), # continuation byte
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1237 (b'\x80\x80', FFFD*2), # 2 continuation bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1238 (b'\xc0', FFFD),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1239 (b'\xc0\xc0', FFFD*2),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1240 (b'\xc1', FFFD),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1241 (b'\xc1\xc0', FFFD*2),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1242 (b'\xc0\xc1', FFFD*2),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1243 # with start byte of a 2-byte sequence
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1244 (b'\xc2', FFFD), # only the start byte
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1245 (b'\xc2\xc2', FFFD*2), # 2 start bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1246 (b'\xc2\xc2\xc2', FFFD*3), # 2 start bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1247 (b'\xc2\x41', FFFD+'A'), # invalid continuation byte
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1248 # with start byte of a 3-byte sequence
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1249 (b'\xe1', FFFD), # only the start byte
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1250 (b'\xe1\xe1', FFFD*2), # 2 start bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1251 (b'\xe1\xe1\xe1', FFFD*3), # 3 start bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1252 (b'\xe1\xe1\xe1\xe1', FFFD*4), # 4 start bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1253 (b'\xe1\x80', FFFD), # only 1 continuation byte
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1254 (b'\xe1\x41', FFFD+'A'), # invalid continuation byte
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1255 (b'\xe1\x41\x80', FFFD+'A'+FFFD), # invalid cb followed by valid cb
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1256 (b'\xe1\x41\x41', FFFD+'AA'), # 2 invalid continuation bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1257 (b'\xe1\x80\x41', FFFD+'A'), # only 1 valid continuation byte
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1258 (b'\xe1\x80\xe1\x41', FFFD*2+'A'), # 1 valid and the other invalid
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1259 (b'\xe1\x41\xe1\x80', FFFD+'A'+FFFD), # 1 invalid and the other valid
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1260 # with start byte of a 4-byte sequence
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1261 (b'\xf1', FFFD), # only the start byte
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1262 (b'\xf1\xf1', FFFD*2), # 2 start bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1263 (b'\xf1\xf1\xf1', FFFD*3), # 3 start bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1264 (b'\xf1\xf1\xf1\xf1', FFFD*4), # 4 start bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1265 (b'\xf1\xf1\xf1\xf1\xf1', FFFD*5), # 5 start bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1266 (b'\xf1\x80', FFFD), # only 1 continuation bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1267 (b'\xf1\x80\x80', FFFD), # only 2 continuation bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1268 (b'\xf1\x80\x41', FFFD+'A'), # 1 valid cb and 1 invalid
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1269 (b'\xf1\x80\x41\x41', FFFD+'AA'), # 1 valid cb and 1 invalid
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1270 (b'\xf1\x80\x80\x41', FFFD+'A'), # 2 valid cb and 1 invalid
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1271 (b'\xf1\x41\x80', FFFD+'A'+FFFD), # 1 invalid cv and 1 valid
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1272 (b'\xf1\x41\x80\x80', FFFD+'A'+FFFD*2), # 1 invalid cb and 2 invalid
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1273 (b'\xf1\x41\x80\x41', FFFD+'A'+FFFD+'A'), # 2 invalid cb and 1 invalid
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1274 (b'\xf1\x41\x41\x80', FFFD+'AA'+FFFD), # 1 valid cb and 1 invalid
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1275 (b'\xf1\x41\xf1\x80', FFFD+'A'+FFFD),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1276 (b'\xf1\x41\x80\xf1', FFFD+'A'+FFFD*2),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1277 (b'\xf1\xf1\x80\x41', FFFD*2+'A'),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1278 (b'\xf1\x41\xf1\xf1', FFFD+'A'+FFFD*2),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1279 # with invalid start byte of a 4-byte sequence (rfc2279)
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1280 (b'\xf5', FFFD), # only the start byte
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1281 (b'\xf5\xf5', FFFD*2), # 2 start bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1282 (b'\xf5\x80', FFFD*2), # only 1 continuation byte
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1283 (b'\xf5\x80\x80', FFFD*3), # only 2 continuation byte
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1284 (b'\xf5\x80\x80\x80', FFFD*4), # 3 continuation bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1285 (b'\xf5\x80\x41', FFFD*2+'A'), # 1 valid cb and 1 invalid
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1286 (b'\xf5\x80\x41\xf5', FFFD*2+'A'+FFFD),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1287 (b'\xf5\x41\x80\x80\x41', FFFD+'A'+FFFD*2+'A'),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1288 # with invalid start byte of a 5-byte sequence (rfc2279)
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1289 (b'\xf8', FFFD), # only the start byte
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1290 (b'\xf8\xf8', FFFD*2), # 2 start bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1291 (b'\xf8\x80', FFFD*2), # only one continuation byte
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1292 (b'\xf8\x80\x41', FFFD*2 + 'A'), # 1 valid cb and 1 invalid
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1293 (b'\xf8\x80\x80\x80\x80', FFFD*5), # invalid 5 bytes seq with 5 bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1294 # with invalid start byte of a 6-byte sequence (rfc2279)
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1295 (b'\xfc', FFFD), # only the start byte
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1296 (b'\xfc\xfc', FFFD*2), # 2 start bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1297 (b'\xfc\x80\x80', FFFD*3), # only 2 continuation bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1298 (b'\xfc\x80\x80\x80\x80\x80', FFFD*6), # 6 continuation bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1299 # invalid start byte
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1300 (b'\xfe', FFFD),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1301 (b'\xfe\x80\x80', FFFD*3),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1302 # other sequences
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1303 (b'\xf1\x80\x41\x42\x43', '\ufffd\x41\x42\x43'),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1304 (b'\xf1\x80\xff\x42\x43', '\ufffd\ufffd\x42\x43'),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1305 (b'\xf1\x80\xc2\x81\x43', '\ufffd\x81\x43'),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1306 (b'\x61\xF1\x80\x80\xE1\x80\xC2\x62\x80\x63\x80\xBF\x64',
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1307 '\x61\uFFFD\uFFFD\uFFFD\x62\uFFFD\x63\uFFFD\uFFFD\x64'),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1308 ]
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1309 for n, (seq, res) in enumerate(sequences):
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1310 self.assertRaises(UnicodeDecodeError, seq.decode, 'utf-8', 'strict')
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1311 self.assertEqual(seq.decode('utf-8', 'replace'), res)
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1312 self.assertEqual((seq+b'b').decode('utf-8', 'replace'), res+'b')
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1313 self.assertEqual(seq.decode('utf-8', 'ignore'),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1314 res.replace('\uFFFD', ''))
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1315
30059
200165d11a94 Support trailing dots in DNS names. Fixes #782510. Will backport to 2.3.
Martin v. Löwis <martin@v.loewis.de>
parents: 29164
diff changeset
1316 def test_codecs_idna(self):
200165d11a94 Support trailing dots in DNS names. Fixes #782510. Will backport to 2.3.
Martin v. Löwis <martin@v.loewis.de>
parents: 29164
diff changeset
1317 # Test whether trailing dot is preserved
41702
e06e196a05cf There's no longer a u prefix for unicode objects
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
1318 self.assertEqual("www.python.org.".encode("idna"), b"www.python.org.")
30059
200165d11a94 Support trailing dots in DNS names. Fixes #782510. Will backport to 2.3.
Martin v. Löwis <martin@v.loewis.de>
parents: 29164
diff changeset
1319
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1320 def test_codecs_errors(self):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1321 # Error handling (encoding)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1322 self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1323 self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii','strict')
41620
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1324 self.assertEqual('Andr\202 x'.encode('ascii','ignore'), b"Andr x")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1325 self.assertEqual('Andr\202 x'.encode('ascii','replace'), b"Andr? x")
56099
1b7bdae02179 Merged revisions 74929 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
1326 self.assertEqual('Andr\202 x'.encode('ascii', 'replace'),
1b7bdae02179 Merged revisions 74929 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
1327 'Andr\202 x'.encode('ascii', errors='replace'))
1b7bdae02179 Merged revisions 74929 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
1328 self.assertEqual('Andr\202 x'.encode('ascii', 'ignore'),
1b7bdae02179 Merged revisions 74929 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
1329 'Andr\202 x'.encode(encoding='ascii', errors='ignore'))
12051
d89556c04ef9 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
1330
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1331 # Error handling (decoding)
41620
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1332 self.assertRaises(UnicodeError, str, b'Andr\202 x', 'ascii')
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1333 self.assertRaises(UnicodeError, str, b'Andr\202 x', 'ascii', 'strict')
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1334 self.assertEqual(str(b'Andr\202 x', 'ascii', 'ignore'), "Andr x")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1335 self.assertEqual(str(b'Andr\202 x', 'ascii', 'replace'), 'Andr\uFFFD x')
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1336
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1337 # Error handling (unknown character names)
42902
62874b34d95d Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42875
diff changeset
1338 self.assertEqual(b"\\N{foo}xx".decode("unicode-escape", "ignore"), "xx")
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1339
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1340 # Error handling (truncated escape sequence)
42907
0170418dadea Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42902
diff changeset
1341 self.assertRaises(UnicodeError, b"\\".decode, "unicode-escape")
12051
d89556c04ef9 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
1342
42907
0170418dadea Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42902
diff changeset
1343 self.assertRaises(TypeError, b"hello".decode, "test.unicode1")
0170418dadea Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42902
diff changeset
1344 self.assertRaises(TypeError, str, b"hello", "test.unicode2")
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1345 self.assertRaises(TypeError, "hello".encode, "test.unicode1")
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1346 self.assertRaises(TypeError, "hello".encode, "test.unicode2")
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1347 # executes PyUnicode_Encode()
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1348 import imp
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1349 self.assertRaises(
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1350 ImportError,
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1351 imp.find_module,
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1352 "non-existing module",
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1353 ["non-existing dir"]
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1354 )
21886
abedb2418f62 Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents: 21882
diff changeset
1355
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1356 # Error handling (wrong arguments)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1357 self.assertRaises(TypeError, "hello".encode, 42, 42, 42)
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1358
66673
7b6af56ee74d Issue #10557: Fixed error messages from float() and other numeric
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66581
diff changeset
1359 # Error handling (lone surrogate in PyUnicode_TransformDecimalToASCII())
7b6af56ee74d Issue #10557: Fixed error messages from float() and other numeric
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66581
diff changeset
1360 self.assertRaises(UnicodeError, int, "\ud800")
7b6af56ee74d Issue #10557: Fixed error messages from float() and other numeric
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66581
diff changeset
1361 self.assertRaises(UnicodeError, int, "\udf00")
7b6af56ee74d Issue #10557: Fixed error messages from float() and other numeric
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66581
diff changeset
1362 self.assertRaises(UnicodeError, float, "\ud800")
7b6af56ee74d Issue #10557: Fixed error messages from float() and other numeric
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66581
diff changeset
1363 self.assertRaises(UnicodeError, float, "\udf00")
7b6af56ee74d Issue #10557: Fixed error messages from float() and other numeric
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66581
diff changeset
1364 self.assertRaises(UnicodeError, complex, "\ud800")
7b6af56ee74d Issue #10557: Fixed error messages from float() and other numeric
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66581
diff changeset
1365 self.assertRaises(UnicodeError, complex, "\udf00")
21886
abedb2418f62 Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents: 21882
diff changeset
1366
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1367 def test_codecs(self):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1368 # Encoding
41620
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1369 self.assertEqual('hello'.encode('ascii'), b'hello')
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1370 self.assertEqual('hello'.encode('utf-7'), b'hello')
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1371 self.assertEqual('hello'.encode('utf-8'), b'hello')
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1372 self.assertEqual('hello'.encode('utf8'), b'hello')
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1373 self.assertEqual('hello'.encode('utf-16-le'), b'h\000e\000l\000l\000o\000')
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1374 self.assertEqual('hello'.encode('utf-16-be'), b'\000h\000e\000l\000l\000o')
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1375 self.assertEqual('hello'.encode('latin-1'), b'hello')
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12074
diff changeset
1376
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1377 # Roundtrip safety for BMP (just the first 1024 chars)
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41620
diff changeset
1378 for c in range(1024):
41585
825d966338ea Rename 'unicode' to 'str' in its tp_name field. Rename 'str' to 'str8'.
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
1379 u = chr(c)
36056
f1ae11457d34 Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents: 35938
diff changeset
1380 for encoding in ('utf-7', 'utf-8', 'utf-16', 'utf-16-le',
f1ae11457d34 Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents: 35938
diff changeset
1381 'utf-16-be', 'raw_unicode_escape',
f1ae11457d34 Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents: 35938
diff changeset
1382 'unicode_escape', 'unicode_internal'):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1383 self.assertEqual(str(u.encode(encoding),encoding), u)
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12074
diff changeset
1384
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1385 # Roundtrip safety for BMP (just the first 256 chars)
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41620
diff changeset
1386 for c in range(256):
41585
825d966338ea Rename 'unicode' to 'str' in its tp_name field. Rename 'str' to 'str8'.
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
1387 u = chr(c)
36056
f1ae11457d34 Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents: 35938
diff changeset
1388 for encoding in ('latin-1',):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1389 self.assertEqual(str(u.encode(encoding),encoding), u)
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12074
diff changeset
1390
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1391 # Roundtrip safety for BMP (just the first 128 chars)
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41620
diff changeset
1392 for c in range(128):
41585
825d966338ea Rename 'unicode' to 'str' in its tp_name field. Rename 'str' to 'str8'.
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
1393 u = chr(c)
36056
f1ae11457d34 Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents: 35938
diff changeset
1394 for encoding in ('ascii',):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1395 self.assertEqual(str(u.encode(encoding),encoding), u)
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 15081
diff changeset
1396
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1397 # Roundtrip safety for non-BMP (just a few chars)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1398 u = '\U00010001\U00020002\U00030003\U00040004\U00050005'
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1399 for encoding in ('utf-8', 'utf-16', 'utf-16-le', 'utf-16-be',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1400 #'raw_unicode_escape',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1401 'unicode_escape', 'unicode_internal'):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1402 self.assertEqual(str(u.encode(encoding),encoding), u)
17815
d6df8d371b67 Get rid of the superstitious "~" in dict hashing's "i = (~hash) & mask".
Tim Peters <tim.peters@gmail.com>
parents: 17655
diff changeset
1403
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1404 # UTF-8 must be roundtrip safe for all UCS-2 code points
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1405 # This excludes surrogates: in the full range, there would be
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1406 # a surrogate pair (\udbff\udc00), which gets converted back
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1407 # to a non-BMP character (\U0010fc00)
41702
e06e196a05cf There's no longer a u prefix for unicode objects
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
1408 u = ''.join(map(chr, list(range(0,0xd800)) +
e06e196a05cf There's no longer a u prefix for unicode objects
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
1409 list(range(0xe000,0x10000))))
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1410 for encoding in ('utf-8',):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1411 self.assertEqual(str(u.encode(encoding),encoding), u)
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1412
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1413 def test_codecs_charmap(self):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1414 # 0-127
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41620
diff changeset
1415 s = bytes(range(128))
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1416 for encoding in (
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1417 'cp037', 'cp1026',
62572
1d1797af4622 Merged revisions 81499,81506 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62171
diff changeset
1418 'cp437', 'cp500', 'cp720', 'cp737', 'cp775', 'cp850',
1d1797af4622 Merged revisions 81499,81506 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62171
diff changeset
1419 'cp852', 'cp855', 'cp858', 'cp860', 'cp861', 'cp862',
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1420 'cp863', 'cp865', 'cp866',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1421 'iso8859_10', 'iso8859_13', 'iso8859_14', 'iso8859_15',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1422 'iso8859_2', 'iso8859_3', 'iso8859_4', 'iso8859_5', 'iso8859_6',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1423 'iso8859_7', 'iso8859_9', 'koi8_r', 'latin_1',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1424 'mac_cyrillic', 'mac_latin2',
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12074
diff changeset
1425
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1426 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1427 'cp1256', 'cp1257', 'cp1258',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1428 'cp856', 'cp857', 'cp864', 'cp869', 'cp874',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1429
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1430 'mac_greek', 'mac_iceland','mac_roman', 'mac_turkish',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1431 'cp1006', 'iso8859_8',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1432
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1433 ### These have undefined mappings:
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1434 #'cp424',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1435
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1436 ### These fail the round-trip:
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1437 #'cp875'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1438
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1439 ):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1440 self.assertEqual(str(s, encoding).encode(encoding), s)
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 15081
diff changeset
1441
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1442 # 128-255
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41620
diff changeset
1443 s = bytes(range(128, 256))
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1444 for encoding in (
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1445 'cp037', 'cp1026',
62572
1d1797af4622 Merged revisions 81499,81506 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62171
diff changeset
1446 'cp437', 'cp500', 'cp720', 'cp737', 'cp775', 'cp850',
1d1797af4622 Merged revisions 81499,81506 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62171
diff changeset
1447 'cp852', 'cp855', 'cp858', 'cp860', 'cp861', 'cp862',
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1448 'cp863', 'cp865', 'cp866',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1449 'iso8859_10', 'iso8859_13', 'iso8859_14', 'iso8859_15',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1450 'iso8859_2', 'iso8859_4', 'iso8859_5',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1451 'iso8859_9', 'koi8_r', 'latin_1',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1452 'mac_cyrillic', 'mac_latin2',
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 15081
diff changeset
1453
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1454 ### These have undefined mappings:
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1455 #'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1456 #'cp1256', 'cp1257', 'cp1258',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1457 #'cp424', 'cp856', 'cp857', 'cp864', 'cp869', 'cp874',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1458 #'iso8859_3', 'iso8859_6', 'iso8859_7',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1459 #'mac_greek', 'mac_iceland','mac_roman', 'mac_turkish',
12390
6edbae6dbcf5 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12365
diff changeset
1460
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1461 ### These fail the round-trip:
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1462 #'cp1006', 'cp875', 'iso8859_8',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1463
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1464 ):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1465 self.assertEqual(str(s, encoding).encode(encoding), s)
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1466
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1467 def test_concatenation(self):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1468 self.assertEqual(("abc" "def"), "abcdef")
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1469 self.assertEqual(("abc" "def"), "abcdef")
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1470 self.assertEqual(("abc" "def"), "abcdef")
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1471 self.assertEqual(("abc" "def" "ghi"), "abcdefghi")
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1472 self.assertEqual(("abc" "def" "ghi"), "abcdefghi")
21027
dee0e866653e Fix for bug #480188: printing unicode objects
Marc-André Lemburg <mal@egenix.com>
parents: 20563
diff changeset
1473
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1474 def test_printing(self):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1475 class BitBucket:
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1476 def write(self, text):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1477 pass
24598
12e146171f3d Committing patch #591250 which provides "str1 in str2" when str1 is a
Barry Warsaw <barry@python.org>
parents: 24538
diff changeset
1478
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1479 out = BitBucket()
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1480 print('abc', file=out)
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1481 print('abc', 'def', file=out)
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1482 print('abc', 'def', file=out)
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1483 print('abc', 'def', file=out)
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1484 print('abc\n', file=out)
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1485 print('abc\n', end=' ', file=out)
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1486 print('abc\n', end=' ', file=out)
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1487 print('def\n', file=out)
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1488 print('def\n', file=out)
24598
12e146171f3d Committing patch #591250 which provides "str1 in str2" when str1 is a
Barry Warsaw <barry@python.org>
parents: 24538
diff changeset
1489
29164
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28917
diff changeset
1490 def test_ucs4(self):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1491 x = '\U00100000'
29164
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28917
diff changeset
1492 y = x.encode("raw-unicode-escape").decode("raw-unicode-escape")
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28917
diff changeset
1493 self.assertEqual(x, y)
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28917
diff changeset
1494
64694
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1495 y = br'\U00100000'
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1496 x = y.decode("raw-unicode-escape").encode("raw-unicode-escape")
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1497 self.assertEqual(x, y)
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1498 y = br'\U00010000'
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1499 x = y.decode("raw-unicode-escape").encode("raw-unicode-escape")
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1500 self.assertEqual(x, y)
46034
0a485593ae1c Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
1501
64694
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1502 try:
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1503 br'\U11111111'.decode("raw-unicode-escape")
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1504 except UnicodeDecodeError as e:
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1505 self.assertEqual(e.start, 0)
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1506 self.assertEqual(e.end, 10)
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1507 else:
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1508 self.fail("Should have raised UnicodeDecodeError")
46034
0a485593ae1c Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
1509
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1510 def test_conversion(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1511 # Make sure __unicode__() works properly
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1512 class Foo0:
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1513 def __str__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1514 return "foo"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1515
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1516 class Foo1:
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
1517 def __str__(self):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1518 return "foo"
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1519
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1520 class Foo2(object):
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
1521 def __str__(self):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1522 return "foo"
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1523
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1524 class Foo3(object):
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
1525 def __str__(self):
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1526 return "foo"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1527
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1528 class Foo4(str):
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
1529 def __str__(self):
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1530 return "foo"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1531
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1532 class Foo5(str):
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
1533 def __str__(self):
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1534 return "foo"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1535
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1536 class Foo6(str):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1537 def __str__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1538 return "foos"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1539
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
1540 def __str__(self):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1541 return "foou"
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1542
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1543 class Foo7(str):
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1544 def __str__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1545 return "foos"
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
1546 def __str__(self):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1547 return "foou"
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1548
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1549 class Foo8(str):
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1550 def __new__(cls, content=""):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1551 return str.__new__(cls, 2*content)
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
1552 def __str__(self):
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1553 return self
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1554
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1555 class Foo9(str):
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1556 def __str__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1557 return "not unicode"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1558
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1559 self.assertEqual(str(Foo0()), "foo")
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1560 self.assertEqual(str(Foo1()), "foo")
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1561 self.assertEqual(str(Foo2()), "foo")
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1562 self.assertEqual(str(Foo3()), "foo")
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1563 self.assertEqual(str(Foo4("bar")), "foo")
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1564 self.assertEqual(str(Foo5("bar")), "foo")
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1565 self.assertEqual(str(Foo6("bar")), "foou")
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1566 self.assertEqual(str(Foo7("bar")), "foou")
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1567 self.assertEqual(str(Foo8("foo")), "foofoo")
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1568 self.assertEqual(str(Foo9("foo")), "not unicode")
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1569
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36341
diff changeset
1570 def test_unicode_repr(self):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36341
diff changeset
1571 class s1:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36341
diff changeset
1572 def __repr__(self):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36341
diff changeset
1573 return '\\n'
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36341
diff changeset
1574
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36341
diff changeset
1575 class s2:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36341
diff changeset
1576 def __repr__(self):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1577 return '\\n'
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36341
diff changeset
1578
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36341
diff changeset
1579 self.assertEqual(repr(s1()), '\\n')
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36341
diff changeset
1580 self.assertEqual(repr(s2()), '\\n')
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36341
diff changeset
1581
64146
8f97c64b2333 #5127: Even on narrow unicode builds, the C functions that access the Unicode
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63939
diff changeset
1582 def test_printable_repr(self):
8f97c64b2333 #5127: Even on narrow unicode builds, the C functions that access the Unicode
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63939
diff changeset
1583 self.assertEqual(repr('\U00010000'), "'%c'" % (0x10000,)) # printable
65217
cbde81ef4af7 Upgrade to Unicode 6.0.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 65059
diff changeset
1584 self.assertEqual(repr('\U00014000'), "'\\U00014000'") # nonprintable
64146
8f97c64b2333 #5127: Even on narrow unicode builds, the C functions that access the Unicode
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63939
diff changeset
1585
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41702
diff changeset
1586 def test_expandtabs_overflows_gracefully(self):
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41702
diff changeset
1587 # This test only affects 32-bit platforms because expandtabs can only take
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41702
diff changeset
1588 # an int as the max value, not a 64-bit C long. If expandtabs is changed
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41702
diff changeset
1589 # to take a 64-bit long, this test should apply to all platforms.
44160
9d152530910a Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents: 44039
diff changeset
1590 if sys.maxsize > (1 << 32) or struct.calcsize('P') != 4:
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41702
diff changeset
1591 return
44160
9d152530910a Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents: 44039
diff changeset
1592 self.assertRaises(OverflowError, 't\tt\t'.expandtabs, sys.maxsize)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36341
diff changeset
1593
48601
a476ef5eebad Merged revisions 65339-65340,65342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48570
diff changeset
1594 def test_raiseMemError(self):
a476ef5eebad Merged revisions 65339-65340,65342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48570
diff changeset
1595 # Ensure that the freelist contains a consistent object, even
a476ef5eebad Merged revisions 65339-65340,65342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48570
diff changeset
1596 # when a string allocation fails with a MemoryError.
a476ef5eebad Merged revisions 65339-65340,65342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48570
diff changeset
1597 # This used to crash the interpreter,
a476ef5eebad Merged revisions 65339-65340,65342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48570
diff changeset
1598 # or leak references when the number was smaller.
49032
97b9f0be9385 Merged revisions 66235 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48723
diff changeset
1599 charwidth = 4 if sys.maxunicode >= 0x10000 else 2
97b9f0be9385 Merged revisions 66235 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48723
diff changeset
1600 # Note: sys.maxsize is half of the actual max allocation because of
97b9f0be9385 Merged revisions 66235 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48723
diff changeset
1601 # the signedness of Py_ssize_t.
97b9f0be9385 Merged revisions 66235 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48723
diff changeset
1602 alloc = lambda: "a" * (sys.maxsize // charwidth * 2)
48723
1d88ad5f4def Merged revisions 65773 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48601
diff changeset
1603 self.assertRaises(MemoryError, alloc)
1d88ad5f4def Merged revisions 65773 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48601
diff changeset
1604 self.assertRaises(MemoryError, alloc)
1d88ad5f4def Merged revisions 65773 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48601
diff changeset
1605
59899
84405c22e4c7 Merged revisions 79281 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 55593
diff changeset
1606 def test_format_subclass(self):
84405c22e4c7 Merged revisions 79281 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 55593
diff changeset
1607 class S(str):
84405c22e4c7 Merged revisions 79281 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 55593
diff changeset
1608 def __str__(self):
84405c22e4c7 Merged revisions 79281 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 55593
diff changeset
1609 return '__str__ overridden'
84405c22e4c7 Merged revisions 79281 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 55593
diff changeset
1610 s = S('xxx')
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65661
diff changeset
1611 self.assertEqual("%s" % s, '__str__ overridden')
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65661
diff changeset
1612 self.assertEqual("{}".format(s), '__str__ overridden')
59899
84405c22e4c7 Merged revisions 79281 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 55593
diff changeset
1613
67363
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
1614 # Test PyUnicode_FromFormat()
64639
1cfad55de5bd Issue #9738: PyUnicode_FromFormat() and PyErr_Format() raise an error on
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64146
diff changeset
1615 def test_from_format(self):
67363
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
1616 support.import_module('ctypes')
68087
ef28aad3bbae Fix PyUnicode_FromFormatV("%c") for non-BMP char
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67791
diff changeset
1617 from ctypes import pythonapi, py_object, c_int
67363
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
1618 if sys.maxunicode == 65535:
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
1619 name = "PyUnicodeUCS2_FromFormat"
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
1620 else:
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
1621 name = "PyUnicodeUCS4_FromFormat"
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
1622 _PyUnicode_FromFormat = getattr(pythonapi, name)
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
1623 _PyUnicode_FromFormat.restype = py_object
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
1624
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
1625 def PyUnicode_FromFormat(format, *args):
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
1626 cargs = tuple(
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
1627 py_object(arg) if isinstance(arg, str) else arg
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
1628 for arg in args)
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
1629 return _PyUnicode_FromFormat(format, *cargs)
64639
1cfad55de5bd Issue #9738: PyUnicode_FromFormat() and PyErr_Format() raise an error on
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64146
diff changeset
1630
1cfad55de5bd Issue #9738: PyUnicode_FromFormat() and PyErr_Format() raise an error on
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64146
diff changeset
1631 # ascii format, non-ascii argument
67363
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
1632 text = PyUnicode_FromFormat(b'ascii\x7f=%U', 'unicode\xe9')
64639
1cfad55de5bd Issue #9738: PyUnicode_FromFormat() and PyErr_Format() raise an error on
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64146
diff changeset
1633 self.assertEqual(text, 'ascii\x7f=unicode\xe9')
1cfad55de5bd Issue #9738: PyUnicode_FromFormat() and PyErr_Format() raise an error on
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64146
diff changeset
1634
67363
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
1635 # non-ascii format, ascii argument: ensure that PyUnicode_FromFormatV()
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
1636 # raises an error
66581
38bec90fd8ab #10273: Rename assertRegexpMatches and assertRaisesRegexp to assertRegex and assertRaisesRegex.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66402
diff changeset
1637 self.assertRaisesRegex(ValueError,
64639
1cfad55de5bd Issue #9738: PyUnicode_FromFormat() and PyErr_Format() raise an error on
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64146
diff changeset
1638 '^PyUnicode_FromFormatV\(\) expects an ASCII-encoded format '
64666
852bb052156a Issue #9738, #9836: Fix refleak introduced by r84704
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64639
diff changeset
1639 'string, got a non-ASCII byte: 0xe9$',
67363
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
1640 PyUnicode_FromFormat, b'unicode\xe9=%s', 'ascii')
48601
a476ef5eebad Merged revisions 65339-65340,65342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48570
diff changeset
1641
68087
ef28aad3bbae Fix PyUnicode_FromFormatV("%c") for non-BMP char
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67791
diff changeset
1642 self.assertEqual(PyUnicode_FromFormat(b'%c', c_int(0xabcd)), '\uabcd')
ef28aad3bbae Fix PyUnicode_FromFormatV("%c") for non-BMP char
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67791
diff changeset
1643 self.assertEqual(PyUnicode_FromFormat(b'%c', c_int(0x10ffff)), '\U0010ffff')
ef28aad3bbae Fix PyUnicode_FromFormatV("%c") for non-BMP char
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67791
diff changeset
1644
65530
3faff7e51f40 PyUnicode_FromFormatV(): Fix %A format
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65217
diff changeset
1645 # other tests
67363
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
1646 text = PyUnicode_FromFormat(b'%%A:%A', 'abc\xe9\uabcd\U0010ffff')
65530
3faff7e51f40 PyUnicode_FromFormatV(): Fix %A format
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65217
diff changeset
1647 self.assertEqual(text, r"%A:'abc\xe9\uabcd\U0010ffff'")
3faff7e51f40 PyUnicode_FromFormatV(): Fix %A format
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65217
diff changeset
1648
68058
72f30f8ef331 Merged revisions 88697 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68087
diff changeset
1649 text = PyUnicode_FromFormat(b'repr=%V', 'abc', b'xyz')
72f30f8ef331 Merged revisions 88697 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68087
diff changeset
1650 self.assertEqual(text, 'repr=abc')
72f30f8ef331 Merged revisions 88697 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68087
diff changeset
1651
72f30f8ef331 Merged revisions 88697 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68087
diff changeset
1652 # Test string decode from parameter of %s using utf-8.
72f30f8ef331 Merged revisions 88697 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68087
diff changeset
1653 # b'\xe4\xba\xba\xe6\xb0\x91' is utf-8 encoded byte sequence of
72f30f8ef331 Merged revisions 88697 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68087
diff changeset
1654 # '\u4eba\u6c11'
72f30f8ef331 Merged revisions 88697 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68087
diff changeset
1655 text = PyUnicode_FromFormat(b'repr=%V', None, b'\xe4\xba\xba\xe6\xb0\x91')
72f30f8ef331 Merged revisions 88697 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68087
diff changeset
1656 self.assertEqual(text, 'repr=\u4eba\u6c11')
72f30f8ef331 Merged revisions 88697 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68087
diff changeset
1657
72f30f8ef331 Merged revisions 88697 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68087
diff changeset
1658 #Test replace error handler.
72f30f8ef331 Merged revisions 88697 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68087
diff changeset
1659 text = PyUnicode_FromFormat(b'repr=%V', None, b'abc\xff')
72f30f8ef331 Merged revisions 88697 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68087
diff changeset
1660 self.assertEqual(text, 'repr=abc\ufffd')
72f30f8ef331 Merged revisions 88697 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68087
diff changeset
1661
65055
de7cdb726350 Issue #8870: PyUnicode_AsWideCharString() doesn't count the trailing nul character
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64724
diff changeset
1662 # Test PyUnicode_AsWideChar()
de7cdb726350 Issue #8870: PyUnicode_AsWideCharString() doesn't count the trailing nul character
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64724
diff changeset
1663 def test_aswidechar(self):
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
1664 from _testcapi import unicode_aswidechar
66402
127f5168a774 Fix tests when ctypes isn't available
Antoine Pitrou <solipsis@pitrou.net>
parents: 66310
diff changeset
1665 support.import_module('ctypes')
65055
de7cdb726350 Issue #8870: PyUnicode_AsWideCharString() doesn't count the trailing nul character
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64724
diff changeset
1666 from ctypes import c_wchar, sizeof
de7cdb726350 Issue #8870: PyUnicode_AsWideCharString() doesn't count the trailing nul character
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64724
diff changeset
1667
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
1668 wchar, size = unicode_aswidechar('abcdef', 2)
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66033
diff changeset
1669 self.assertEqual(size, 2)
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66033
diff changeset
1670 self.assertEqual(wchar, 'ab')
65055
de7cdb726350 Issue #8870: PyUnicode_AsWideCharString() doesn't count the trailing nul character
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64724
diff changeset
1671
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
1672 wchar, size = unicode_aswidechar('abc', 3)
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66033
diff changeset
1673 self.assertEqual(size, 3)
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66033
diff changeset
1674 self.assertEqual(wchar, 'abc')
65055
de7cdb726350 Issue #8870: PyUnicode_AsWideCharString() doesn't count the trailing nul character
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64724
diff changeset
1675
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
1676 wchar, size = unicode_aswidechar('abc', 4)
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66033
diff changeset
1677 self.assertEqual(size, 3)
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66033
diff changeset
1678 self.assertEqual(wchar, 'abc\0')
65055
de7cdb726350 Issue #8870: PyUnicode_AsWideCharString() doesn't count the trailing nul character
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64724
diff changeset
1679
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
1680 wchar, size = unicode_aswidechar('abc', 10)
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66033
diff changeset
1681 self.assertEqual(size, 3)
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66033
diff changeset
1682 self.assertEqual(wchar, 'abc\0')
65055
de7cdb726350 Issue #8870: PyUnicode_AsWideCharString() doesn't count the trailing nul character
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64724
diff changeset
1683
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
1684 wchar, size = unicode_aswidechar('abc\0def', 20)
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66033
diff changeset
1685 self.assertEqual(size, 7)
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66033
diff changeset
1686 self.assertEqual(wchar, 'abc\0def\0')
65055
de7cdb726350 Issue #8870: PyUnicode_AsWideCharString() doesn't count the trailing nul character
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64724
diff changeset
1687
65056
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
1688 nonbmp = chr(0x10ffff)
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
1689 if sizeof(c_wchar) == 2:
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
1690 buflen = 3
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
1691 nchar = 2
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
1692 else: # sizeof(c_wchar) == 4
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
1693 buflen = 2
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
1694 nchar = 1
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
1695 wchar, size = unicode_aswidechar(nonbmp, buflen)
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66033
diff changeset
1696 self.assertEqual(size, nchar)
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66033
diff changeset
1697 self.assertEqual(wchar, nonbmp + '\0')
65056
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
1698
65055
de7cdb726350 Issue #8870: PyUnicode_AsWideCharString() doesn't count the trailing nul character
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64724
diff changeset
1699 # Test PyUnicode_AsWideCharString()
de7cdb726350 Issue #8870: PyUnicode_AsWideCharString() doesn't count the trailing nul character
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64724
diff changeset
1700 def test_aswidecharstring(self):
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
1701 from _testcapi import unicode_aswidecharstring
66402
127f5168a774 Fix tests when ctypes isn't available
Antoine Pitrou <solipsis@pitrou.net>
parents: 66310
diff changeset
1702 support.import_module('ctypes')
65055
de7cdb726350 Issue #8870: PyUnicode_AsWideCharString() doesn't count the trailing nul character
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64724
diff changeset
1703 from ctypes import c_wchar, sizeof
de7cdb726350 Issue #8870: PyUnicode_AsWideCharString() doesn't count the trailing nul character
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64724
diff changeset
1704
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
1705 wchar, size = unicode_aswidecharstring('abc')
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66033
diff changeset
1706 self.assertEqual(size, 3)
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66033
diff changeset
1707 self.assertEqual(wchar, 'abc\0')
65055
de7cdb726350 Issue #8870: PyUnicode_AsWideCharString() doesn't count the trailing nul character
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64724
diff changeset
1708
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
1709 wchar, size = unicode_aswidecharstring('abc\0def')
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66033
diff changeset
1710 self.assertEqual(size, 7)
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66033
diff changeset
1711 self.assertEqual(wchar, 'abc\0def\0')
65055
de7cdb726350 Issue #8870: PyUnicode_AsWideCharString() doesn't count the trailing nul character
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64724
diff changeset
1712
65056
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
1713 nonbmp = chr(0x10ffff)
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
1714 if sizeof(c_wchar) == 2:
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
1715 nchar = 2
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
1716 else: # sizeof(c_wchar) == 4
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
1717 nchar = 1
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
1718 wchar, size = unicode_aswidecharstring(nonbmp)
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66033
diff changeset
1719 self.assertEqual(size, nchar)
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66033
diff changeset
1720 self.assertEqual(wchar, nonbmp + '\0')
65056
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
1721
65055
de7cdb726350 Issue #8870: PyUnicode_AsWideCharString() doesn't count the trailing nul character
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64724
diff changeset
1722
67791
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1723 class StringModuleTest(unittest.TestCase):
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1724 def test_formatter_parser(self):
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1725 def parse(format):
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1726 return list(_string.formatter_parser(format))
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1727
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1728 formatter = parse("prefix {2!s}xxx{0:^+10.3f}{obj.attr!s} {z[0]!s:10}")
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1729 self.assertEqual(formatter, [
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1730 ('prefix ', '2', '', 's'),
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1731 ('xxx', '0', '^+10.3f', None),
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1732 ('', 'obj.attr', '', 's'),
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1733 (' ', 'z[0]', '10', 's'),
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1734 ])
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1735
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1736 formatter = parse("prefix {} suffix")
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1737 self.assertEqual(formatter, [
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1738 ('prefix ', '', '', None),
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1739 (' suffix', None, None, None),
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1740 ])
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1741
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1742 formatter = parse("str")
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1743 self.assertEqual(formatter, [
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1744 ('str', None, None, None),
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1745 ])
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1746
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1747 formatter = parse("")
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1748 self.assertEqual(formatter, [])
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1749
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1750 formatter = parse("{0}")
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1751 self.assertEqual(formatter, [
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1752 ('', '0', '', None),
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1753 ])
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1754
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1755 self.assertRaises(TypeError, _string.formatter_parser, 1)
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1756
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1757 def test_formatter_field_name_split(self):
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1758 def split(name):
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1759 items = list(_string.formatter_field_name_split(name))
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1760 items[1] = list(items[1])
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1761 return items
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1762 self.assertEqual(split("obj"), ["obj", []])
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1763 self.assertEqual(split("obj.arg"), ["obj", [(True, 'arg')]])
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1764 self.assertEqual(split("obj[key]"), ["obj", [(False, 'key')]])
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1765 self.assertEqual(split("obj.arg[key1][key2]"), [
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1766 "obj",
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1767 [(True, 'arg'),
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1768 (False, 'key1'),
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1769 (False, 'key2'),
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1770 ]])
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1771 self.assertRaises(TypeError, _string.formatter_field_name_split, 1)
3ef865327d7b Issue #11302: missing type check on _string.formatter_field_name_split and _string.formatter_parser caused crash.
Eric Smith <eric@trueblade.com>
parents: 67363
diff changeset
1772
73680
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1773 def test_encode_decimal(self):
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1774 from _testcapi import unicode_encodedecimal
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1775 self.assertEqual(unicode_encodedecimal('123'),
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1776 b'123')
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1777 self.assertEqual(unicode_encodedecimal('\u0663.\u0661\u0664'),
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1778 b'3.14')
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1779 self.assertEqual(unicode_encodedecimal("\N{EM SPACE}3.14\N{EN SPACE}"),
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1780 b' 3.14 ')
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1781 self.assertRaises(UnicodeEncodeError,
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1782 unicode_encodedecimal, "123\u20ac", "strict")
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1783 self.assertEqual(unicode_encodedecimal("123\u20ac", "replace"),
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1784 b'123?')
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1785 self.assertEqual(unicode_encodedecimal("123\u20ac", "ignore"),
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1786 b'123')
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1787 self.assertEqual(unicode_encodedecimal("123\u20ac", "xmlcharrefreplace"),
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1788 b'123&#8364;')
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1789 self.assertEqual(unicode_encodedecimal("123\u20ac", "backslashreplace"),
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1790 b'123\\u20ac')
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1791 self.assertEqual(unicode_encodedecimal("123\u20ac\N{EM SPACE}", "replace"),
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1792 b'123? ')
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1793 self.assertEqual(unicode_encodedecimal("123\u20ac\u20ac", "replace"),
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1794 b'123??')
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1795 self.assertEqual(unicode_encodedecimal("123\u20ac\u0660", "replace"),
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1796 b'123?0')
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1797
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1798 def test_transform_decimal(self):
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1799 from _testcapi import unicode_transformdecimaltoascii as transform_decimal
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1800 self.assertEqual(transform_decimal('123'),
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1801 '123')
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1802 self.assertEqual(transform_decimal('\u0663.\u0661\u0664'),
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1803 '3.14')
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1804 self.assertEqual(transform_decimal("\N{EM SPACE}3.14\N{EN SPACE}"),
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1805 "\N{EM SPACE}3.14\N{EN SPACE}")
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1806 self.assertEqual(transform_decimal('123\u20ac'),
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1807 '123\u20ac')
f29d7d597fae Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73552
diff changeset
1808
48601
a476ef5eebad Merged revisions 65339-65340,65342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48570
diff changeset
1809
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1810 def test_main():
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46307
diff changeset
1811 support.run_unittest(__name__)
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1812
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1813 if __name__ == "__main__":
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1814 test_main()