annotate Lib/test/test_unicode.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents e27684eed3b6
children
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 """#"
73579
ec8c0ac81706 "unicode_internal" codec has been deprecated: fix related tests
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73553
diff changeset
8 import _string
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
9 import codecs
83203
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
10 import itertools
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
11 import struct
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
12 import sys
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
13 import unittest
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
14 import warnings
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46307
diff changeset
15 from test import support, string_tests
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
16
35938
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
17 # 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
18 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
19 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
20 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
21 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
22 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
23 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
24 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
25 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
26 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
27 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
28 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
29 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
30 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
31 else:
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
32 return None
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
33 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
34
83203
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
35 def duplicate_string(text):
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
36 """
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
37 Try to get a fresh clone of the specified text:
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
38 new object with a reference count of 1.
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
39
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
40 This is a best-effort: latin1 single letters and the empty
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
41 string ('') are singletons and cannot be cloned.
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
42 """
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
43 return text.encode().decode()
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
44
59775
e619047a2f36 Clean up the warnings filter use in test_unicode.
Brett Cannon <bcannon@gmail.com>
parents: 58415
diff changeset
45 class UnicodeTest(string_tests.CommonTest,
e619047a2f36 Clean up the warnings filter use in test_unicode.
Brett Cannon <bcannon@gmail.com>
parents: 58415
diff changeset
46 string_tests.MixinStrUnicodeUserStringTest,
81356
55383912c225 #16910: test_bytes, test_unicode, and test_userstring now work with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80542
diff changeset
47 string_tests.MixinStrUnicodeTest,
55383912c225 #16910: test_bytes, test_unicode, and test_userstring now work with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80542
diff changeset
48 unittest.TestCase):
59775
e619047a2f36 Clean up the warnings filter use in test_unicode.
Brett Cannon <bcannon@gmail.com>
parents: 58415
diff changeset
49
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
50 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
51
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
52 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
53 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
54 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
55 self.assertEqual(realresult, result)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54484
diff changeset
56 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
57
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
58 # 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
59 # 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
60 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
61 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
62 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
63 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
64 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
65 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
66 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
67 self.assertEqual(realresult, result)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54484
diff changeset
68 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
69
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 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
71 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
72 self.assertEqual('\uffff', '\U0000ffff')
42324
4bbbe0c55fa2 Merged revisions 56443-56466 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42313
diff changeset
73 self.assertRaises(SyntaxError, eval, '\'\\Ufffffffe\'')
4bbbe0c55fa2 Merged revisions 56443-56466 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42313
diff changeset
74 self.assertRaises(SyntaxError, eval, '\'\\Uffffffff\'')
4bbbe0c55fa2 Merged revisions 56443-56466 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42313
diff changeset
75 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
76 # 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
77 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
78
47611
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
79 def test_ascii(self):
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
80 if not sys.platform.startswith('java'):
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
81 # Test basic sanity of repr()
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
82 self.assertEqual(ascii('abc'), "'abc'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
83 self.assertEqual(ascii('ab\\c'), "'ab\\\\c'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
84 self.assertEqual(ascii('ab\\'), "'ab\\\\'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
85 self.assertEqual(ascii('\\c'), "'\\\\c'")
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 self.assertEqual(ascii('\n'), "'\\n'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
88 self.assertEqual(ascii('\r'), "'\\r'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
89 self.assertEqual(ascii('\t'), "'\\t'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
90 self.assertEqual(ascii('\b'), "'\\x08'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
91 self.assertEqual(ascii("'\""), """'\\'"'""")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
92 self.assertEqual(ascii("'\""), """'\\'"'""")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
93 self.assertEqual(ascii("'"), '''"'"''')
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
94 self.assertEqual(ascii('"'), """'"'""")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
95 latin1repr = (
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
96 "'\\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
97 "\\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
98 "\\x1b\\x1c\\x1d\\x1e\\x1f !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHI"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
99 "JKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f"
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
100 "\\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
101 "\\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
102 "\\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
103 "\\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
104 "\\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
105 "\\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
106 "\\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
107 "\\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
108 "\\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
109 "\\xfe\\xff'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
110 testrepr = ascii(''.join(map(chr, range(256))))
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
111 self.assertEqual(testrepr, latin1repr)
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
112 # Test ascii works on wide unicode escapes without overflow.
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
113 self.assertEqual(ascii("\U00010000" * 39 + "\uffff" * 4096),
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
114 ascii("\U00010000" * 39 + "\uffff" * 4096))
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
115
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
116 class WrongRepr:
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
117 def __repr__(self):
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
118 return b'byte-repr'
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
119 self.assertRaises(TypeError, ascii, WrongRepr())
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
120
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
121 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
122 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
123 # 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
124 self.assertEqual(repr('abc'), "'abc'")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
125 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
126 self.assertEqual(repr('ab\\'), "'ab\\\\'")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
127 self.assertEqual(repr('\\c'), "'\\\\c'")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
128 self.assertEqual(repr('\\'), "'\\\\'")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
129 self.assertEqual(repr('\n'), "'\\n'")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
130 self.assertEqual(repr('\r'), "'\\r'")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
131 self.assertEqual(repr('\t'), "'\\t'")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
132 self.assertEqual(repr('\b'), "'\\x08'")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
133 self.assertEqual(repr("'\""), """'\\'"'""")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
134 self.assertEqual(repr("'\""), """'\\'"'""")
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
135 self.assertEqual(repr("'"), '''"'"''')
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
136 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
137 latin1repr = (
41620
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
138 "'\\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
139 "\\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
140 "\\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
141 "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
142 "\\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
143 "\\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
144 "\\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
145 "\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
146 "\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
147 "\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
148 "\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
149 "\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
150 "\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
151 "\xfe\xff'")
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41620
diff changeset
152 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
153 self.assertEqual(testrepr, latin1repr)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39544
diff changeset
154 # 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
155 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
156 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
157
47611
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
158 class WrongRepr:
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
159 def __repr__(self):
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
160 return b'byte-repr'
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
161 self.assertRaises(TypeError, repr, WrongRepr())
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
162
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
163 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
164 # 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
165 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
166 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
167 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
168 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
169 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
170
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
171 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
172 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
173 # 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
174 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
175 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
176 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
177 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
178 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
179 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
180 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
181 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
182 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
183
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
184 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
185 string_tests.CommonTest.test_find(self)
72859
af4a89f4f466 Issue #13134: optimize finding single-character strings using memchr
Antoine Pitrou <solipsis@pitrou.net>
parents: 72814
diff changeset
186 # test implementation details of the memchr fast path
af4a89f4f466 Issue #13134: optimize finding single-character strings using memchr
Antoine Pitrou <solipsis@pitrou.net>
parents: 72814
diff changeset
187 self.checkequal(100, 'a' * 100 + '\u0102', 'find', '\u0102')
af4a89f4f466 Issue #13134: optimize finding single-character strings using memchr
Antoine Pitrou <solipsis@pitrou.net>
parents: 72814
diff changeset
188 self.checkequal(-1, 'a' * 100 + '\u0102', 'find', '\u0201')
af4a89f4f466 Issue #13134: optimize finding single-character strings using memchr
Antoine Pitrou <solipsis@pitrou.net>
parents: 72814
diff changeset
189 self.checkequal(-1, 'a' * 100 + '\u0102', 'find', '\u0120')
af4a89f4f466 Issue #13134: optimize finding single-character strings using memchr
Antoine Pitrou <solipsis@pitrou.net>
parents: 72814
diff changeset
190 self.checkequal(-1, 'a' * 100 + '\u0102', 'find', '\u0220')
af4a89f4f466 Issue #13134: optimize finding single-character strings using memchr
Antoine Pitrou <solipsis@pitrou.net>
parents: 72814
diff changeset
191 self.checkequal(100, 'a' * 100 + '\U00100304', 'find', '\U00100304')
af4a89f4f466 Issue #13134: optimize finding single-character strings using memchr
Antoine Pitrou <solipsis@pitrou.net>
parents: 72814
diff changeset
192 self.checkequal(-1, 'a' * 100 + '\U00100304', 'find', '\U00100204')
af4a89f4f466 Issue #13134: optimize finding single-character strings using memchr
Antoine Pitrou <solipsis@pitrou.net>
parents: 72814
diff changeset
193 self.checkequal(-1, 'a' * 100 + '\U00100304', 'find', '\U00102004')
af4a89f4f466 Issue #13134: optimize finding single-character strings using memchr
Antoine Pitrou <solipsis@pitrou.net>
parents: 72814
diff changeset
194 # 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
195 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
196 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
197 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
198
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
199 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
200 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
201
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
202 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
203 string_tests.CommonTest.test_rfind(self)
72859
af4a89f4f466 Issue #13134: optimize finding single-character strings using memchr
Antoine Pitrou <solipsis@pitrou.net>
parents: 72814
diff changeset
204 # test implementation details of the memrchr fast path
af4a89f4f466 Issue #13134: optimize finding single-character strings using memchr
Antoine Pitrou <solipsis@pitrou.net>
parents: 72814
diff changeset
205 self.checkequal(0, '\u0102' + 'a' * 100 , 'rfind', '\u0102')
af4a89f4f466 Issue #13134: optimize finding single-character strings using memchr
Antoine Pitrou <solipsis@pitrou.net>
parents: 72814
diff changeset
206 self.checkequal(-1, '\u0102' + 'a' * 100 , 'rfind', '\u0201')
af4a89f4f466 Issue #13134: optimize finding single-character strings using memchr
Antoine Pitrou <solipsis@pitrou.net>
parents: 72814
diff changeset
207 self.checkequal(-1, '\u0102' + 'a' * 100 , 'rfind', '\u0120')
af4a89f4f466 Issue #13134: optimize finding single-character strings using memchr
Antoine Pitrou <solipsis@pitrou.net>
parents: 72814
diff changeset
208 self.checkequal(-1, '\u0102' + 'a' * 100 , 'rfind', '\u0220')
af4a89f4f466 Issue #13134: optimize finding single-character strings using memchr
Antoine Pitrou <solipsis@pitrou.net>
parents: 72814
diff changeset
209 self.checkequal(0, '\U00100304' + 'a' * 100, 'rfind', '\U00100304')
af4a89f4f466 Issue #13134: optimize finding single-character strings using memchr
Antoine Pitrou <solipsis@pitrou.net>
parents: 72814
diff changeset
210 self.checkequal(-1, '\U00100304' + 'a' * 100, 'rfind', '\U00100204')
af4a89f4f466 Issue #13134: optimize finding single-character strings using memchr
Antoine Pitrou <solipsis@pitrou.net>
parents: 72814
diff changeset
211 self.checkequal(-1, '\U00100304' + 'a' * 100, 'rfind', '\U00102004')
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
212 # 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
213 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
214 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
215 self.checkequalnofix(12, 'abcdefghiabc', 'rfind', '')
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
216
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
217 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
218 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
219 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
220 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
221 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
222 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
223 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
224 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
225 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
226 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
227
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
228 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
229 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
230 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
231 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
232 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
233 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
234
41592
1e5a2a2b18ee Fix various spots where int/long and str/unicode unification
Walter Dörwald <walter@livinglogic.de>
parents: 41585
diff changeset
235 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
236 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
237 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
238 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
239 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
240
44039
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
241 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
242 # 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
243 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
244 {ord('a'): None})
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
245 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
246 {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
247 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
248 {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
249 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
250 {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
251 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
252 {ord('z'): 'yy'})
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
253 # this needs maketrans()
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
254 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
255 {'b': '<i>'})
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
256 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
257 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
258 # 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
259 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
260 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
261
12e728035e2c #1496: revert str.translate() to the old version, and add
Georg Brandl <georg@python.org>
parents: 43947
diff changeset
262 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
263 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
264 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
265 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
266 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
267 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
268 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
269
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
270 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
271 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
272
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
273 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
274 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
275
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
276 # Mixed arguments
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
277 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
278 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
279 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
280
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
281 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
282 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
283
43446
a7c48ab809ec Patch # 1145 by Thomas Lee:
Guido van Rossum <guido@python.org>
parents: 43241
diff changeset
284 class MyWrapper:
a7c48ab809ec Patch # 1145 by Thomas Lee:
Guido van Rossum <guido@python.org>
parents: 43241
diff changeset
285 def __init__(self, sval): self.sval = sval
a7c48ab809ec Patch # 1145 by Thomas Lee:
Guido van Rossum <guido@python.org>
parents: 43241
diff changeset
286 def __str__(self): return self.sval
a7c48ab809ec Patch # 1145 by Thomas Lee:
Guido van Rossum <guido@python.org>
parents: 43241
diff changeset
287
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
288 # mixed arguments
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
289 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
290 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
291 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
292 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
293 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
294 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
295 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
296 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
297 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
298 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
299 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
300
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
301 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
302 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
303
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
304 # 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
305 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
306 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
307
72784
9c1b76936b79 str.replace(a, a) is now returning str unchanged if a is a
Victor Stinner <vstinner@wyplay.com>
parents: 72666
diff changeset
308 @support.cpython_only
9c1b76936b79 str.replace(a, a) is now returning str unchanged if a is a
Victor Stinner <vstinner@wyplay.com>
parents: 72666
diff changeset
309 def test_replace_id(self):
72788
e3d5b718f2e6 Mark 'abc'.expandtab() optimization as specific to CPython
Victor Stinner <vstinner@wyplay.com>
parents: 72784
diff changeset
310 pattern = 'abc'
e3d5b718f2e6 Mark 'abc'.expandtab() optimization as specific to CPython
Victor Stinner <vstinner@wyplay.com>
parents: 72784
diff changeset
311 text = 'abc def'
e3d5b718f2e6 Mark 'abc'.expandtab() optimization as specific to CPython
Victor Stinner <vstinner@wyplay.com>
parents: 72784
diff changeset
312 self.assertIs(text.replace(pattern, pattern), text)
72784
9c1b76936b79 str.replace(a, a) is now returning str unchanged if a is a
Victor Stinner <vstinner@wyplay.com>
parents: 72666
diff changeset
313
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
314 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
315 with support.check_warnings():
e619047a2f36 Clean up the warnings filter use in test_unicode.
Brett Cannon <bcannon@gmail.com>
parents: 58415
diff changeset
316 warnings.simplefilter('ignore', BytesWarning)
e619047a2f36 Clean up the warnings filter use in test_unicode.
Brett Cannon <bcannon@gmail.com>
parents: 58415
diff changeset
317 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
318 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
319 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
320 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
321
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
322 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
323 # Comparisons:
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
324 self.assertEqual('abc', 'abc')
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54484
diff changeset
325 self.assertTrue('abcd' > 'abc')
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54484
diff changeset
326 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
327
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
328 if 0:
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
329 # 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
330 # 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
331
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
332 # No surrogates, no fixup required.
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54484
diff changeset
333 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
334 # 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
335 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
336
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
337 # 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
338 def test_lecmp(s, s2):
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54484
diff changeset
339 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
340
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
341 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
342 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
343 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
344 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
345 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
346 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
347 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
348 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
349 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
350 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
351 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
352 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
353 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
354 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
355 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
356 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
357 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
358 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
359 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
360 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
361 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
362 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
363 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
364 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
365 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
366 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
367 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
368 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
369 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
370 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
371 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
372 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
373 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
374
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
375 test_fixup('\ue000')
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
376 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
377
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
378 # Surrogates on both sides, no fixup required
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54484
diff changeset
379 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
380
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
381 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
382 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
383 self.checkequalnofix(False, '\u1FFc', 'islower')
74338
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
384 self.assertFalse('\u2167'.islower())
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
385 self.assertTrue('\u2177'.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
386 # 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
387 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
388 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
389 # 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
390 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
391 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
392 # 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
393 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
394 self.assertFalse('\U0001F46F'.islower())
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
395
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
396 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
397 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
398 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
399 self.checkequalnofix(False, '\u1FFc', 'isupper')
74338
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
400 self.assertTrue('\u2167'.isupper())
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
401 self.assertFalse('\u2177'.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
402 # 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
403 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
404 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
405 # 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
406 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
407 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
408 # 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
409 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
410 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
411
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
412 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
413 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
414 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
415 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
416
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
417 # 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
418 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
419 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
420 # 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
421 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
422 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
423
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
424 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
425 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
426 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
427 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
428 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
429 # 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
430 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
431 '\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
432 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
433
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 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
435 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
436 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
437 '\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
438 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
439
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
440 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
441 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
442 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
443 # 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
444 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
445 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
446 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
447 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
448 # 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
449 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
450 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
451
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
452 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
453 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
454 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
455 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
456 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
457 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
458 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
459 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
460 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
461
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
462 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
463
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
464 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
465 '\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
466 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
467 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
468 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
469
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
470 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
471 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
472 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
473 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
474 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
475
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
476 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
477 '\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
478 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
479 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
480 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
481
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
482 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
483 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
484 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
485 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
486 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
487 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
488 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
489 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
490 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
491
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
492 self.assertRaises(TypeError, "abc".isnumeric, 42)
12917
2a2caaee6797 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12897
diff changeset
493
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
494 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
495 '\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
496 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
497 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
498 '\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
499 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
500
42597
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
501 def test_isidentifier(self):
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
502 self.assertTrue("a".isidentifier())
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
503 self.assertTrue("Z".isidentifier())
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
504 self.assertTrue("_".isidentifier())
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
505 self.assertTrue("b0".isidentifier())
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
506 self.assertTrue("bc".isidentifier())
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
507 self.assertTrue("b_".isidentifier())
65661
e9e3fe4c9e30 Merged revisions 85861 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63941
diff changeset
508 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
509 self.assertTrue("𝔘𝔫𝔦𝔠𝔬𝔡𝔢".isidentifier())
42597
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
510
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
511 self.assertFalse(" ".isidentifier())
20477cbfd996 Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents: 42324
diff changeset
512 self.assertFalse("[".isidentifier())
65661
e9e3fe4c9e30 Merged revisions 85861 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63941
diff changeset
513 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
514 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
515
47611
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
516 def test_isprintable(self):
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
517 self.assertTrue("".isprintable())
52345
07b6fcdf7f94 fix isprintable() on space characters #5126
Benjamin Peterson <benjamin@python.org>
parents: 52159
diff changeset
518 self.assertTrue(" ".isprintable())
47611
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
519 self.assertTrue("abcdefg".isprintable())
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
520 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
521 # 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
522 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
523 # 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
524 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
525 # single surrogate character
47611
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
526 self.assertFalse("\ud800".isprintable())
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
527
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
528 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
529 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
530
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 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
532 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
533 '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
534 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
535 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
536 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
537 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
538 '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
539 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
540 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
541 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
542
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
543 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
544 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
545 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
546 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
547
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 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
549 '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
550 '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
551 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
552 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
553 '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
554 '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
555 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
556
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
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
558 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
559 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
560 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
561 self.assertEqual('\U00010427\U00010427'.lower(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
562 '\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
563 self.assertEqual('\U00010427\U0001044F'.lower(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
564 '\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
565 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
566 'x\U0001044Fx\U0001044F')
74338
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
567 self.assertEqual('fi'.lower(), 'fi')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
568 self.assertEqual('\u0130'.lower(), '\u0069\u0307')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
569 # Special case for GREEK CAPITAL LETTER SIGMA U+03A3
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
570 self.assertEqual('\u03a3'.lower(), '\u03c3')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
571 self.assertEqual('\u0345\u03a3'.lower(), '\u0345\u03c3')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
572 self.assertEqual('A\u0345\u03a3'.lower(), 'a\u0345\u03c2')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
573 self.assertEqual('A\u0345\u03a3a'.lower(), 'a\u0345\u03c3a')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
574 self.assertEqual('A\u0345\u03a3'.lower(), 'a\u0345\u03c2')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
575 self.assertEqual('A\u03a3\u0345'.lower(), 'a\u03c2\u0345')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
576 self.assertEqual('\u03a3\u0345 '.lower(), '\u03c3\u0345 ')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
577 self.assertEqual('\U0008fffe'.lower(), '\U0008fffe')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
578 self.assertEqual('\u2177'.lower(), '\u2177')
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
579
74405
d4669f43d05f add str.casefold() (closes #13752)
Benjamin Peterson <benjamin@python.org>
parents: 74338
diff changeset
580 def test_casefold(self):
d4669f43d05f add str.casefold() (closes #13752)
Benjamin Peterson <benjamin@python.org>
parents: 74338
diff changeset
581 self.assertEqual('hello'.casefold(), 'hello')
d4669f43d05f add str.casefold() (closes #13752)
Benjamin Peterson <benjamin@python.org>
parents: 74338
diff changeset
582 self.assertEqual('hELlo'.casefold(), 'hello')
d4669f43d05f add str.casefold() (closes #13752)
Benjamin Peterson <benjamin@python.org>
parents: 74338
diff changeset
583 self.assertEqual('ß'.casefold(), 'ss')
d4669f43d05f add str.casefold() (closes #13752)
Benjamin Peterson <benjamin@python.org>
parents: 74338
diff changeset
584 self.assertEqual('fi'.casefold(), 'fi')
d4669f43d05f add str.casefold() (closes #13752)
Benjamin Peterson <benjamin@python.org>
parents: 74338
diff changeset
585 self.assertEqual('\u03a3'.casefold(), '\u03c3')
d4669f43d05f add str.casefold() (closes #13752)
Benjamin Peterson <benjamin@python.org>
parents: 74338
diff changeset
586 self.assertEqual('A\u0345\u03a3'.casefold(), 'a\u03b9\u03c3')
78440
cde674716de4 add another testcase
Benjamin Peterson <benjamin@python.org>
parents: 76890
diff changeset
587 self.assertEqual('\u00b5'.casefold(), '\u03bc')
74405
d4669f43d05f add str.casefold() (closes #13752)
Benjamin Peterson <benjamin@python.org>
parents: 74338
diff changeset
588
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
589 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
590 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
591 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
592 self.assertEqual('\U0001044F\U0001044F'.upper(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
593 '\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
594 self.assertEqual('\U00010427\U0001044F'.upper(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
595 '\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
596 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
597 'X\U00010427X\U00010427')
74338
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
598 self.assertEqual('fi'.upper(), 'FI')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
599 self.assertEqual('\u0130'.upper(), '\u0130')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
600 self.assertEqual('\u03a3'.upper(), '\u03a3')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
601 self.assertEqual('ß'.upper(), 'SS')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
602 self.assertEqual('\u1fd2'.upper(), '\u0399\u0308\u0300')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
603 self.assertEqual('\U0008fffe'.upper(), '\U0008fffe')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
604 self.assertEqual('\u2177'.upper(), '\u2167')
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
605
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
606 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
607 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
608 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
609 self.assertEqual('\U0001044F\U0001044F'.capitalize(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
610 '\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
611 self.assertEqual('\U00010427\U0001044F'.capitalize(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
612 '\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
613 self.assertEqual('\U0001044F\U00010427'.capitalize(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
614 '\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
615 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
616 'X\U0001044Fx\U0001044F')
74338
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
617 self.assertEqual('h\u0130'.capitalize(), 'H\u0069\u0307')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
618 exp = '\u0399\u0308\u0300\u0069\u0307'
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
619 self.assertEqual('\u1fd2\u0130'.capitalize(), exp)
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
620 self.assertEqual('finnish'.capitalize(), 'FInnish')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
621 self.assertEqual('A\u0345\u03a3'.capitalize(), 'A\u0345\u03c2')
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
622
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
623 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
624 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
625 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
626 self.assertEqual('\U0001044F\U0001044F'.title(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
627 '\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
628 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
629 '\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
630 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
631 '\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
632 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
633 '\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
634 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
635 'X\U0001044Fx\U0001044F X\U0001044Fx\U0001044F')
74338
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
636 self.assertEqual('fiNNISH'.title(), 'Finnish')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
637 self.assertEqual('A\u03a3 \u1fa1xy'.title(), 'A\u03c2 \u1fa9xy')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
638 self.assertEqual('A\u03a3A'.title(), 'A\u03c3a')
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
639
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
640 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
641 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
642 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
643 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
644 self.assertEqual('\U0001044F\U0001044F'.swapcase(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
645 '\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
646 self.assertEqual('\U00010427\U0001044F'.swapcase(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
647 '\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
648 self.assertEqual('\U0001044F\U00010427'.swapcase(),
72040
af952a67cee1 Fix indentation and add a skip decorator.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72035
diff changeset
649 '\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
650 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
651 'x\U0001044FX\U00010427')
74338
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
652 self.assertEqual('fi'.swapcase(), 'FI')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
653 self.assertEqual('\u0130'.swapcase(), '\u0069\u0307')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
654 # Special case for GREEK CAPITAL LETTER SIGMA U+03A3
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
655 self.assertEqual('\u03a3'.swapcase(), '\u03c3')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
656 self.assertEqual('\u0345\u03a3'.swapcase(), '\u0399\u03c3')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
657 self.assertEqual('A\u0345\u03a3'.swapcase(), 'a\u0399\u03c2')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
658 self.assertEqual('A\u0345\u03a3a'.swapcase(), 'a\u0399\u03c3A')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
659 self.assertEqual('A\u0345\u03a3'.swapcase(), 'a\u0399\u03c2')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
660 self.assertEqual('A\u03a3\u0345'.swapcase(), 'a\u03c2\u0399')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
661 self.assertEqual('\u03a3\u0345 '.swapcase(), '\u03c3\u0399 ')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
662 self.assertEqual('\u03a3'.swapcase(), '\u03c3')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
663 self.assertEqual('ß'.swapcase(), 'SS')
f7e05d205a52 use full unicode mappings for upper/lower/title case (#12736)
Benjamin Peterson <benjamin@python.org>
parents: 73741
diff changeset
664 self.assertEqual('\u1fd2'.swapcase(), '\u0399\u0308\u0300')
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
665
84503
d249203f423c Add a couple of tests for str.center with non-ASCII chars.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 84281
diff changeset
666 def test_center(self):
d249203f423c Add a couple of tests for str.center with non-ASCII chars.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 84281
diff changeset
667 string_tests.CommonTest.test_center(self)
d249203f423c Add a couple of tests for str.center with non-ASCII chars.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 84281
diff changeset
668 self.assertEqual('x'.center(2, '\U0010FFFF'),
d249203f423c Add a couple of tests for str.center with non-ASCII chars.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 84281
diff changeset
669 'x\U0010FFFF')
d249203f423c Add a couple of tests for str.center with non-ASCII chars.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 84281
diff changeset
670 self.assertEqual('x'.center(3, '\U0010FFFF'),
d249203f423c Add a couple of tests for str.center with non-ASCII chars.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 84281
diff changeset
671 '\U0010FFFFx\U0010FFFF')
d249203f423c Add a couple of tests for str.center with non-ASCII chars.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 84281
diff changeset
672 self.assertEqual('x'.center(4, '\U0010FFFF'),
d249203f423c Add a couple of tests for str.center with non-ASCII chars.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 84281
diff changeset
673 '\U0010FFFFx\U0010FFFF\U0010FFFF')
d249203f423c Add a couple of tests for str.center with non-ASCII chars.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 84281
diff changeset
674
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
675 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
676 # Testing Unicode contains method
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
677 self.assertIn('a', 'abdb')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
678 self.assertIn('a', 'bdab')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
679 self.assertIn('a', 'bdaba')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
680 self.assertIn('a', 'bdba')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
681 self.assertNotIn('a', 'bdb')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
682 self.assertIn('a', 'bdba')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
683 self.assertIn('a', ('a',1,None))
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
684 self.assertIn('a', (1,None,'a'))
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
685 self.assertIn('a', ('a',1,None))
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
686 self.assertIn('a', (1,None,'a'))
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
687 self.assertNotIn('a', ('x',1,'y'))
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
688 self.assertNotIn('a', ('x',1,None))
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
689 self.assertNotIn('abcd', 'abcxxxx')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
690 self.assertIn('ab', 'abcd')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
691 self.assertIn('ab', 'abc')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
692 self.assertIn('ab', (1,None,'ab'))
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
693 self.assertIn('', 'abc')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
694 self.assertIn('', '')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
695 self.assertIn('', 'abc')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
696 self.assertNotIn('\0', 'abc')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
697 self.assertIn('\0', '\0abc')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
698 self.assertIn('\0', 'abc\0')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
699 self.assertIn('a', '\0abc')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
700 self.assertIn('asdf', 'asdf')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
701 self.assertNotIn('asdf', 'asd')
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 56099
diff changeset
702 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
703
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
704 self.assertRaises(TypeError, "abc".__contains__)
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
705
84100
b11507395ce4 Add tests for issue #18183.
Serhiy Storchaka <storchaka@gmail.com>
parents: 84083
diff changeset
706 def test_issue18183(self):
b11507395ce4 Add tests for issue #18183.
Serhiy Storchaka <storchaka@gmail.com>
parents: 84083
diff changeset
707 '\U00010000\U00100000'.lower()
b11507395ce4 Add tests for issue #18183.
Serhiy Storchaka <storchaka@gmail.com>
parents: 84083
diff changeset
708 '\U00010000\U00100000'.casefold()
b11507395ce4 Add tests for issue #18183.
Serhiy Storchaka <storchaka@gmail.com>
parents: 84083
diff changeset
709 '\U00010000\U00100000'.upper()
b11507395ce4 Add tests for issue #18183.
Serhiy Storchaka <storchaka@gmail.com>
parents: 84083
diff changeset
710 '\U00010000\U00100000'.capitalize()
b11507395ce4 Add tests for issue #18183.
Serhiy Storchaka <storchaka@gmail.com>
parents: 84083
diff changeset
711 '\U00010000\U00100000'.title()
b11507395ce4 Add tests for issue #18183.
Serhiy Storchaka <storchaka@gmail.com>
parents: 84083
diff changeset
712 '\U00010000\U00100000'.swapcase()
b11507395ce4 Add tests for issue #18183.
Serhiy Storchaka <storchaka@gmail.com>
parents: 84083
diff changeset
713 '\U00100000'.center(3, '\U00010000')
b11507395ce4 Add tests for issue #18183.
Serhiy Storchaka <storchaka@gmail.com>
parents: 84083
diff changeset
714 '\U00100000'.ljust(3, '\U00010000')
b11507395ce4 Add tests for issue #18183.
Serhiy Storchaka <storchaka@gmail.com>
parents: 84083
diff changeset
715 '\U00100000'.rjust(3, '\U00010000')
b11507395ce4 Add tests for issue #18183.
Serhiy Storchaka <storchaka@gmail.com>
parents: 84083
diff changeset
716
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
717 def test_format(self):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
718 self.assertEqual(''.format(), '')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
719 self.assertEqual('a'.format(), 'a')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
720 self.assertEqual('ab'.format(), 'ab')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
721 self.assertEqual('a{{'.format(), 'a{')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
722 self.assertEqual('a}}'.format(), 'a}')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
723 self.assertEqual('{{b'.format(), '{b')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
724 self.assertEqual('}}b'.format(), '}b')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
725 self.assertEqual('a{{b'.format(), 'a{b')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
726
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
727 # examples from the PEP:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
728 import datetime
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
729 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
730 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
731 "My name is Fred")
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
732 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
733 "My name is Fred :-{}")
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
734
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
735 d = datetime.date(2007, 8, 18)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
736 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
737 "The year is 2007")
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
738
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
739 # classes we'll use for testing
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
740 class C:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
741 def __init__(self, x=100):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
742 self._x = x
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
743 def __format__(self, spec):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
744 return spec
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
745
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
746 class D:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
747 def __init__(self, x):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
748 self.x = x
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
749 def __format__(self, spec):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
750 return str(self.x)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
751
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
752 # class with __str__, but no __format__
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
753 class E:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
754 def __init__(self, x):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
755 self.x = x
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
756 def __str__(self):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
757 return 'E(' + self.x + ')'
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
758
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
759 # 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
760 class F:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
761 def __init__(self, x):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
762 self.x = x
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
763 def __repr__(self):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
764 return 'F(' + self.x + ')'
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
765
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
766 # 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
767 class G:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
768 def __init__(self, x):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
769 self.x = x
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
770 def __str__(self):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
771 return "string is " + self.x
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
772 def __format__(self, format_spec):
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
773 if format_spec == 'd':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
774 return 'G(' + self.x + ')'
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
775 return object.__format__(self, format_spec)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
776
42909
7a62c2469dea Additional test for formatting code.
Eric Smith <eric@trueblade.com>
parents: 42907
diff changeset
777 class I(datetime.date):
7a62c2469dea Additional test for formatting code.
Eric Smith <eric@trueblade.com>
parents: 42907
diff changeset
778 def __format__(self, format_spec):
7a62c2469dea Additional test for formatting code.
Eric Smith <eric@trueblade.com>
parents: 42907
diff changeset
779 return self.strftime(format_spec)
7a62c2469dea Additional test for formatting code.
Eric Smith <eric@trueblade.com>
parents: 42907
diff changeset
780
43097
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42909
diff changeset
781 class J(int):
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42909
diff changeset
782 def __format__(self, format_spec):
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42909
diff changeset
783 return int.__format__(self * 2, format_spec)
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42909
diff changeset
784
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
785
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
786 self.assertEqual(''.format(), '')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
787 self.assertEqual('abc'.format(), 'abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
788 self.assertEqual('{0}'.format('abc'), 'abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
789 self.assertEqual('{0:}'.format('abc'), 'abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
790 # self.assertEqual('{ 0 }'.format('abc'), 'abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
791 self.assertEqual('X{0}'.format('abc'), 'Xabc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
792 self.assertEqual('{0}X'.format('abc'), 'abcX')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
793 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
794 self.assertEqual('{1}'.format(1, 'abc'), 'abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
795 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
796 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
797 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
798 self.assertEqual('{0}'.format(-15), '-15')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
799 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
800 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
801 self.assertEqual('{{'.format(), '{')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
802 self.assertEqual('}}'.format(), '}')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
803 self.assertEqual('{{}}'.format(), '{}')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
804 self.assertEqual('{{x}}'.format(), '{x}')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
805 self.assertEqual('{{{0}}}'.format(123), '{123}')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
806 self.assertEqual('{{{{0}}}}'.format(), '{{0}}')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
807 self.assertEqual('}}{{'.format(), '}{')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
808 self.assertEqual('}}x{{'.format(), '}x{')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
809
42875
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
810 # 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
811 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
812 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
813 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
814
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
815 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
816 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
817 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
818 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
819 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
820 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
821 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
822
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
823 # strings
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
824 self.assertEqual('{0:.3s}'.format('abc'), 'abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
825 self.assertEqual('{0:.3s}'.format('ab'), 'ab')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
826 self.assertEqual('{0:.3s}'.format('abcdef'), 'abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
827 self.assertEqual('{0:.0s}'.format('abcdef'), '')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
828 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
829 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
830 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
831 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
832 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
833 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
834 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
835 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
836 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
837 self.assertEqual('{0: <7s}'.format('result'), 'result ')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
838 self.assertEqual('{0:<7s}'.format('result'), 'result ')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
839 self.assertEqual('{0:>7s}'.format('result'), ' result')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
840 self.assertEqual('{0:>8s}'.format('result'), ' result')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
841 self.assertEqual('{0:^8s}'.format('result'), ' result ')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
842 self.assertEqual('{0:^9s}'.format('result'), ' result ')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
843 self.assertEqual('{0:^10s}'.format('result'), ' result ')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
844 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
845 self.assertEqual('{0:10000}'.format(''), ' ' * 10000)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
846 self.assertEqual('{0:10000000}'.format(''), ' ' * 10000000)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
847
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
848 # format specifiers for user defined type
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
849 self.assertEqual('{0:abc}'.format(C()), 'abc')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
850
48427
4e6859c6d38c Issue #3282: str.isprintable() should return False for undefined Unicode characters.
Georg Brandl <georg@python.org>
parents: 47611
diff changeset
851 # !r, !s and !a coercions
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
852 self.assertEqual('{0!s}'.format('Hello'), 'Hello')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
853 self.assertEqual('{0!s:}'.format('Hello'), 'Hello')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
854 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
855 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
856 self.assertEqual('{0!r}'.format('Hello'), "'Hello'")
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
857 self.assertEqual('{0!r:}'.format('Hello'), "'Hello'")
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
858 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
859 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
860 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
861 self.assertEqual('{0!r}'.format(F('\u0374')), 'F(\u0374)')
47611
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
862 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
863 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
864 self.assertEqual('{0!a}'.format('\u0374'), "'\\u0374'") # printable
47611
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
865 self.assertEqual('{0!a:}'.format('Hello'), "'Hello'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
866 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
867 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
868
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
869 # test fallback to object.__format__
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
870 self.assertEqual('{0}'.format({}), '{}')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
871 self.assertEqual('{0}'.format([]), '[]')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
872 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
873
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
874 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
875 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
876
80991
d91c14788729 Issue #9856: Replace deprecation warinigs to raising TypeError in object.__format__
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80543
diff changeset
877 self.assertRaises(TypeError, '{0:^10}'.format, E('data'))
d91c14788729 Issue #9856: Replace deprecation warinigs to raising TypeError in object.__format__
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80543
diff changeset
878 self.assertRaises(TypeError, '{0:^10s}'.format, E('data'))
d91c14788729 Issue #9856: Replace deprecation warinigs to raising TypeError in object.__format__
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80543
diff changeset
879 self.assertRaises(TypeError, '{0:>15s}'.format, G('data'))
64724
9f55faaa3193 Issue 7994: Make object.__format__() raise a PendingDeprecationWarning
Eric Smith <eric@trueblade.com>
parents: 64694
diff changeset
880
42909
7a62c2469dea Additional test for formatting code.
Eric Smith <eric@trueblade.com>
parents: 42907
diff changeset
881 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
882 month=8,
7a62c2469dea Additional test for formatting code.
Eric Smith <eric@trueblade.com>
parents: 42907
diff changeset
883 day=27)),
7a62c2469dea Additional test for formatting code.
Eric Smith <eric@trueblade.com>
parents: 42907
diff changeset
884 "date: 2007-08-27")
7a62c2469dea Additional test for formatting code.
Eric Smith <eric@trueblade.com>
parents: 42907
diff changeset
885
43097
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42909
diff changeset
886 # test deriving from a builtin type and overriding __format__
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42909
diff changeset
887 self.assertEqual("{0}".format(J(10)), "20")
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42909
diff changeset
888
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42909
diff changeset
889
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
890 # string format specifiers
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
891 self.assertEqual('{0:}'.format('a'), 'a')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
892
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
893 # computed format specifiers
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
894 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
895 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
896 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
897 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
898 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
899
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
900 # test various errors
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
901 self.assertRaises(ValueError, '{'.format)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
902 self.assertRaises(ValueError, '}'.format)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
903 self.assertRaises(ValueError, 'a{'.format)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
904 self.assertRaises(ValueError, 'a}'.format)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
905 self.assertRaises(ValueError, '{a'.format)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
906 self.assertRaises(ValueError, '}a'.format)
43241
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
907 self.assertRaises(IndexError, '{0}'.format)
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
908 self.assertRaises(IndexError, '{1}'.format, 'abc')
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
909 self.assertRaises(KeyError, '{x}'.format)
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
910 self.assertRaises(ValueError, "}{".format)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
911 self.assertRaises(ValueError, "abc{0:{}".format)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
912 self.assertRaises(ValueError, "{0".format)
43241
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
913 self.assertRaises(IndexError, "{0.}".format)
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
914 self.assertRaises(ValueError, "{0.}".format, 0)
83819
f914130c15bb rewrite the parsing of field names to be more consistent wrt recursive expansion
Benjamin Peterson <benjamin@python.org>
parents: 83818
diff changeset
915 self.assertRaises(ValueError, "{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
916 self.assertRaises(ValueError, "{0[}".format, [])
43241
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
917 self.assertRaises(KeyError, "{0]}".format)
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
918 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
919 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
920 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
921 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
922 self.assertRaises(KeyError, "{c]}".format)
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
923 self.assertRaises(ValueError, "{{ {{{0}}".format, 0)
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
924 self.assertRaises(ValueError, "{0}}".format, 0)
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
925 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
926 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
927 self.assertRaises(ValueError, "{0!}".format, 0)
e946073dee38 Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents: 43206
diff changeset
928 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
929 self.assertRaises(ValueError, "{!}".format)
52159
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
930 self.assertRaises(IndexError, "{:}".format)
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
931 self.assertRaises(IndexError, "{:s}".format)
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
932 self.assertRaises(IndexError, "{}".format)
62185
b5af5c93135b Merged revisions 81823,81835 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59899
diff changeset
933 big = "23098475029384702983476098230754973209482573"
b5af5c93135b Merged revisions 81823,81835 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59899
diff changeset
934 self.assertRaises(ValueError, ("{" + big + "}").format)
b5af5c93135b Merged revisions 81823,81835 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59899
diff changeset
935 self.assertRaises(ValueError, ("{[" + big + "]}").format, [0])
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
936
54269
2bcac08cfc6d Merged revisions 72848 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 53965
diff changeset
937 # issue 6089
2bcac08cfc6d Merged revisions 72848 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 53965
diff changeset
938 self.assertRaises(ValueError, "{0[0]x}".format, [None])
2bcac08cfc6d Merged revisions 72848 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 53965
diff changeset
939 self.assertRaises(ValueError, "{0[0](10)}".format, [None])
2bcac08cfc6d Merged revisions 72848 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 53965
diff changeset
940
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
941 # 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
942 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
943
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
944 # exceed maximum recursion depth
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
945 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
946 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
947 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
948
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
949 # string format spec errors
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
950 self.assertRaises(ValueError, "{0:-s}".format, '')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
951 self.assertRaises(ValueError, format, "", "-")
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
952 self.assertRaises(ValueError, "{0:=s}".format, '')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42597
diff changeset
953
48472
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48430
diff changeset
954 # Alternate formatting is not supported
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48430
diff changeset
955 self.assertRaises(ValueError, format, '', '#')
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48430
diff changeset
956 self.assertRaises(ValueError, format, '', '#20')
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48430
diff changeset
957
76509
139c3ae84772 Close #14648: Compute correctly maxchar in str.format() for substrin
Victor Stinner <victor.stinner@gmail.com>
parents: 76484
diff changeset
958 # Non-ASCII
139c3ae84772 Close #14648: Compute correctly maxchar in str.format() for substrin
Victor Stinner <victor.stinner@gmail.com>
parents: 76484
diff changeset
959 self.assertEqual("{0:s}{1:s}".format("ABC", "\u0410\u0411\u0412"),
139c3ae84772 Close #14648: Compute correctly maxchar in str.format() for substrin
Victor Stinner <victor.stinner@gmail.com>
parents: 76484
diff changeset
960 'ABC\u0410\u0411\u0412')
139c3ae84772 Close #14648: Compute correctly maxchar in str.format() for substrin
Victor Stinner <victor.stinner@gmail.com>
parents: 76484
diff changeset
961 self.assertEqual("{0:.3s}".format("ABC\u0410\u0411\u0412"),
139c3ae84772 Close #14648: Compute correctly maxchar in str.format() for substrin
Victor Stinner <victor.stinner@gmail.com>
parents: 76484
diff changeset
962 'ABC')
139c3ae84772 Close #14648: Compute correctly maxchar in str.format() for substrin
Victor Stinner <victor.stinner@gmail.com>
parents: 76484
diff changeset
963 self.assertEqual("{0:.0s}".format("ABC\u0410\u0411\u0412"),
139c3ae84772 Close #14648: Compute correctly maxchar in str.format() for substrin
Victor Stinner <victor.stinner@gmail.com>
parents: 76484
diff changeset
964 '')
139c3ae84772 Close #14648: Compute correctly maxchar in str.format() for substrin
Victor Stinner <victor.stinner@gmail.com>
parents: 76484
diff changeset
965
83817
6786e681ed58 only recursively expand in the format spec (closes #17644)
Benjamin Peterson <benjamin@python.org>
parents: 82285
diff changeset
966 self.assertEqual("{[{}]}".format({"{}": 5}), "5")
83819
f914130c15bb rewrite the parsing of field names to be more consistent wrt recursive expansion
Benjamin Peterson <benjamin@python.org>
parents: 83818
diff changeset
967 self.assertEqual("{[{}]}".format({"{}" : "a"}), "a")
f914130c15bb rewrite the parsing of field names to be more consistent wrt recursive expansion
Benjamin Peterson <benjamin@python.org>
parents: 83818
diff changeset
968 self.assertEqual("{[{]}".format({"{" : "a"}), "a")
f914130c15bb rewrite the parsing of field names to be more consistent wrt recursive expansion
Benjamin Peterson <benjamin@python.org>
parents: 83818
diff changeset
969 self.assertEqual("{[}]}".format({"}" : "a"}), "a")
f914130c15bb rewrite the parsing of field names to be more consistent wrt recursive expansion
Benjamin Peterson <benjamin@python.org>
parents: 83818
diff changeset
970 self.assertEqual("{[[]}".format({"[" : "a"}), "a")
f914130c15bb rewrite the parsing of field names to be more consistent wrt recursive expansion
Benjamin Peterson <benjamin@python.org>
parents: 83818
diff changeset
971 self.assertEqual("{[!]}".format({"!" : "a"}), "a")
f914130c15bb rewrite the parsing of field names to be more consistent wrt recursive expansion
Benjamin Peterson <benjamin@python.org>
parents: 83818
diff changeset
972 self.assertRaises(ValueError, "{a{}b}".format, 42)
f914130c15bb rewrite the parsing of field names to be more consistent wrt recursive expansion
Benjamin Peterson <benjamin@python.org>
parents: 83818
diff changeset
973 self.assertRaises(ValueError, "{a{b}".format, 42)
f914130c15bb rewrite the parsing of field names to be more consistent wrt recursive expansion
Benjamin Peterson <benjamin@python.org>
parents: 83818
diff changeset
974 self.assertRaises(ValueError, "{[}".format, 42)
83817
6786e681ed58 only recursively expand in the format spec (closes #17644)
Benjamin Peterson <benjamin@python.org>
parents: 82285
diff changeset
975
87605
bab7dc2ffc16 fix format spec recursive expansion (closes #19729)
Benjamin Peterson <benjamin@python.org>
parents: 85003
diff changeset
976 self.assertEqual("0x{:0{:d}X}".format(0x0,16), "0x0000000000000000")
83817
6786e681ed58 only recursively expand in the format spec (closes #17644)
Benjamin Peterson <benjamin@python.org>
parents: 82285
diff changeset
977
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
978 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
979 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
980 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
981 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
982 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
983 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
984 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
985 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
986 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
987
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
988 # 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
989 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
990 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
991 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
992 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
993 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
994
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
995 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
996 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
997 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
998 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
999 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
1000 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
1001
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
1002
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
1003 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
1004 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
1005 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
1006 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
1007 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
1008 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
1009
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
1010 # 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
1011 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
1012 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
1013
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
1014 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
1015 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
1016 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
1017 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
1018 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
1019 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
1020
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
1021 # 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
1022 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
1023 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
1024 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
1025
80015
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1026 def test_format_huge_precision(self):
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1027 format_string = ".{}f".format(sys.maxsize + 1)
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1028 with self.assertRaises(ValueError):
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1029 result = format(2.34, format_string)
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1030
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1031 def test_format_huge_width(self):
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1032 format_string = "{}f".format(sys.maxsize + 1)
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1033 with self.assertRaises(ValueError):
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1034 result = format(2.34, format_string)
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1035
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1036 def test_format_huge_item_number(self):
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1037 format_string = "{{{}:.6f}}".format(sys.maxsize + 1)
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1038 with self.assertRaises(ValueError):
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1039 result = format_string.format(2.34)
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1040
52159
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1041 def test_format_auto_numbering(self):
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1042 class C:
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1043 def __init__(self, x=100):
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1044 self._x = x
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1045 def __format__(self, spec):
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1046 return spec
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1047
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1048 self.assertEqual('{}'.format(10), '10')
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1049 self.assertEqual('{:5}'.format('s'), 's ')
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1050 self.assertEqual('{!r}'.format('s'), "'s'")
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1051 self.assertEqual('{._x}'.format(C(10)), '10')
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1052 self.assertEqual('{[1]}'.format([1, 2]), '2')
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1053 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
1054 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
1055
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1056 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
1057 self.assertEqual('a{:{}x}b'.format(20, '#'), 'a0x14b')
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1058
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1059 # 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
1060 self.assertRaises(ValueError, '{}{1}'.format, 1, 2)
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1061 self.assertRaises(ValueError, '{1}{}'.format, 1, 2)
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1062 self.assertRaises(ValueError, '{:{1}}'.format, 1, 2)
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1063 self.assertRaises(ValueError, '{0:{}}'.format, 1, 2)
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1064
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1065 # can mix and match auto-numbering and named
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1066 self.assertEqual('{f}{}'.format(4, f='test'), 'test4')
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1067 self.assertEqual('{}{f}'.format(4, f='test'), '4test')
1c0a0f7b4f76 Merged revisions 70364 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 49142
diff changeset
1068 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
1069 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
1070
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1071 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
1072 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
1073 # 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
1074 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
1075 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
1076 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
1077 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
1078 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
1079 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
1080 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
1081 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
1082 self.assertEqual("%r" % ("\u1234",), "'\u1234'")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
1083 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
1084 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
1085 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
1086
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1087 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
1088 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
1089 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
1090 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
1091 self.assertRaises(TypeError, "%c".__mod__, "aa")
63101
5cff058d6ced Merged revisions 82978 via svnmerge from
Stefan Krah <stefan@bytereef.org>
parents: 62710
diff changeset
1092 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
1093 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
1094
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1095 # 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
1096 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
1097 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
1098 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
1099 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
1100 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
1101 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
1102 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
1103 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
1104 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
1105 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
1106 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
1107 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
1108 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
1109 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
1110 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
1111 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
1112 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
1113 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
1114 return '\u1234'
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1115 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
1116
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
1117 # 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
1118 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
1119 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
1120 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
1121 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
1122 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
1123 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
1124
76841
6d39b5ea4542 unicode_writer_finish() checks string consistency
Victor Stinner <victor.stinner@gmail.com>
parents: 76509
diff changeset
1125 # PEP 393
6d39b5ea4542 unicode_writer_finish() checks string consistency
Victor Stinner <victor.stinner@gmail.com>
parents: 76509
diff changeset
1126 self.assertEqual('%.1s' % "a\xe9\u20ac", 'a')
6d39b5ea4542 unicode_writer_finish() checks string consistency
Victor Stinner <victor.stinner@gmail.com>
parents: 76509
diff changeset
1127 self.assertEqual('%.2s' % "a\xe9\u20ac", 'a\xe9')
6d39b5ea4542 unicode_writer_finish() checks string consistency
Victor Stinner <victor.stinner@gmail.com>
parents: 76509
diff changeset
1128
85473
33727fbb4668 Close #18780: %-formatting now prints value for int subclasses with %d, %i, and %u codes.
Ethan Furman <ethan@stoneleaf.us>
parents: 85004
diff changeset
1129 def test_formatting_with_enum(self):
33727fbb4668 Close #18780: %-formatting now prints value for int subclasses with %d, %i, and %u codes.
Ethan Furman <ethan@stoneleaf.us>
parents: 85004
diff changeset
1130 # issue18780
33727fbb4668 Close #18780: %-formatting now prints value for int subclasses with %d, %i, and %u codes.
Ethan Furman <ethan@stoneleaf.us>
parents: 85004
diff changeset
1131 import enum
33727fbb4668 Close #18780: %-formatting now prints value for int subclasses with %d, %i, and %u codes.
Ethan Furman <ethan@stoneleaf.us>
parents: 85004
diff changeset
1132 class Float(float, enum.Enum):
33727fbb4668 Close #18780: %-formatting now prints value for int subclasses with %d, %i, and %u codes.
Ethan Furman <ethan@stoneleaf.us>
parents: 85004
diff changeset
1133 PI = 3.1415926
33727fbb4668 Close #18780: %-formatting now prints value for int subclasses with %d, %i, and %u codes.
Ethan Furman <ethan@stoneleaf.us>
parents: 85004
diff changeset
1134 class Int(enum.IntEnum):
33727fbb4668 Close #18780: %-formatting now prints value for int subclasses with %d, %i, and %u codes.
Ethan Furman <ethan@stoneleaf.us>
parents: 85004
diff changeset
1135 IDES = 15
33727fbb4668 Close #18780: %-formatting now prints value for int subclasses with %d, %i, and %u codes.
Ethan Furman <ethan@stoneleaf.us>
parents: 85004
diff changeset
1136 class Str(str, enum.Enum):
33727fbb4668 Close #18780: %-formatting now prints value for int subclasses with %d, %i, and %u codes.
Ethan Furman <ethan@stoneleaf.us>
parents: 85004
diff changeset
1137 ABC = 'abc'
33727fbb4668 Close #18780: %-formatting now prints value for int subclasses with %d, %i, and %u codes.
Ethan Furman <ethan@stoneleaf.us>
parents: 85004
diff changeset
1138 # Testing Unicode formatting strings...
85475
833246d42825 Issue #18780: code cleanup.
Ethan Furman <ethan@stoneleaf.us>
parents: 85473
diff changeset
1139 self.assertEqual("%s, %s" % (Str.ABC, Str.ABC),
833246d42825 Issue #18780: code cleanup.
Ethan Furman <ethan@stoneleaf.us>
parents: 85473
diff changeset
1140 'Str.ABC, Str.ABC')
833246d42825 Issue #18780: code cleanup.
Ethan Furman <ethan@stoneleaf.us>
parents: 85473
diff changeset
1141 self.assertEqual("%s, %s, %d, %i, %u, %f, %5.2f" %
833246d42825 Issue #18780: code cleanup.
Ethan Furman <ethan@stoneleaf.us>
parents: 85473
diff changeset
1142 (Str.ABC, Str.ABC,
833246d42825 Issue #18780: code cleanup.
Ethan Furman <ethan@stoneleaf.us>
parents: 85473
diff changeset
1143 Int.IDES, Int.IDES, Int.IDES,
833246d42825 Issue #18780: code cleanup.
Ethan Furman <ethan@stoneleaf.us>
parents: 85473
diff changeset
1144 Float.PI, Float.PI),
833246d42825 Issue #18780: code cleanup.
Ethan Furman <ethan@stoneleaf.us>
parents: 85473
diff changeset
1145 'Str.ABC, Str.ABC, 15, 15, 15, 3.141593, 3.14')
85473
33727fbb4668 Close #18780: %-formatting now prints value for int subclasses with %d, %i, and %u codes.
Ethan Furman <ethan@stoneleaf.us>
parents: 85004
diff changeset
1146
33727fbb4668 Close #18780: %-formatting now prints value for int subclasses with %d, %i, and %u codes.
Ethan Furman <ethan@stoneleaf.us>
parents: 85004
diff changeset
1147 # formatting jobs delegated from the string implementation:
85475
833246d42825 Issue #18780: code cleanup.
Ethan Furman <ethan@stoneleaf.us>
parents: 85473
diff changeset
1148 self.assertEqual('...%(foo)s...' % {'foo':Str.ABC},
833246d42825 Issue #18780: code cleanup.
Ethan Furman <ethan@stoneleaf.us>
parents: 85473
diff changeset
1149 '...Str.ABC...')
833246d42825 Issue #18780: code cleanup.
Ethan Furman <ethan@stoneleaf.us>
parents: 85473
diff changeset
1150 self.assertEqual('...%(foo)s...' % {'foo':Int.IDES},
833246d42825 Issue #18780: code cleanup.
Ethan Furman <ethan@stoneleaf.us>
parents: 85473
diff changeset
1151 '...Int.IDES...')
833246d42825 Issue #18780: code cleanup.
Ethan Furman <ethan@stoneleaf.us>
parents: 85473
diff changeset
1152 self.assertEqual('...%(foo)i...' % {'foo':Int.IDES},
833246d42825 Issue #18780: code cleanup.
Ethan Furman <ethan@stoneleaf.us>
parents: 85473
diff changeset
1153 '...15...')
833246d42825 Issue #18780: code cleanup.
Ethan Furman <ethan@stoneleaf.us>
parents: 85473
diff changeset
1154 self.assertEqual('...%(foo)d...' % {'foo':Int.IDES},
833246d42825 Issue #18780: code cleanup.
Ethan Furman <ethan@stoneleaf.us>
parents: 85473
diff changeset
1155 '...15...')
833246d42825 Issue #18780: code cleanup.
Ethan Furman <ethan@stoneleaf.us>
parents: 85473
diff changeset
1156 self.assertEqual('...%(foo)u...' % {'foo':Int.IDES, 'def':Float.PI},
833246d42825 Issue #18780: code cleanup.
Ethan Furman <ethan@stoneleaf.us>
parents: 85473
diff changeset
1157 '...15...')
833246d42825 Issue #18780: code cleanup.
Ethan Furman <ethan@stoneleaf.us>
parents: 85473
diff changeset
1158 self.assertEqual('...%(foo)f...' % {'foo':Float.PI,'def':123},
833246d42825 Issue #18780: code cleanup.
Ethan Furman <ethan@stoneleaf.us>
parents: 85473
diff changeset
1159 '...3.141593...')
85473
33727fbb4668 Close #18780: %-formatting now prints value for int subclasses with %d, %i, and %u codes.
Ethan Furman <ethan@stoneleaf.us>
parents: 85004
diff changeset
1160
80015
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1161 @support.cpython_only
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1162 def test_formatting_huge_precision(self):
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1163 from _testcapi import INT_MAX
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1164 format_string = "%.{}f".format(INT_MAX + 1)
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1165 with self.assertRaises(ValueError):
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1166 result = format_string % 2.34
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1167
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1168 def test_formatting_huge_width(self):
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1169 format_string = "%{}f".format(sys.maxsize + 1)
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1170 with self.assertRaises(ValueError):
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1171 result = format_string % 2.34
102df748572d Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 75558
diff changeset
1172
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
1173 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
1174 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
1175 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
1176 meth(['f'])
69564
f393c507717a #6780: merge with 3.1.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68257 69563
diff changeset
1177 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
1178 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
1179 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
1180
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46307
diff changeset
1181 @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
1182 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: 37301
diff changeset
1183 # 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
1184 self.assertEqual('1.0', '%.1f' % 1.0)
36341
4bcf5fe54d83 Checkin the test of patch #1400181.
Georg Brandl <georg@python.org>
parents: 36056
diff changeset
1185
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1186 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
1187 # 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
1188
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1189 self.assertEqual(
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1190 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
1191 '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
1192 )
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1193
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1194 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
1195 pass
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 18828
diff changeset
1196
72566
756001a37949 Optimize unicode_subtype_new(): don't encode to wchar_t and decode from wchar_t
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72558
diff changeset
1197 for text in ('ascii', '\xe9', '\u20ac', '\U0010FFFF'):
756001a37949 Optimize unicode_subtype_new(): don't encode to wchar_t and decode from wchar_t
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72558
diff changeset
1198 subclass = UnicodeSubclass(text)
756001a37949 Optimize unicode_subtype_new(): don't encode to wchar_t and decode from wchar_t
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72558
diff changeset
1199 self.assertEqual(str(subclass), text)
756001a37949 Optimize unicode_subtype_new(): don't encode to wchar_t and decode from wchar_t
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72558
diff changeset
1200 self.assertEqual(len(subclass), len(text))
756001a37949 Optimize unicode_subtype_new(): don't encode to wchar_t and decode from wchar_t
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72558
diff changeset
1201 if text == 'ascii':
756001a37949 Optimize unicode_subtype_new(): don't encode to wchar_t and decode from wchar_t
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72558
diff changeset
1202 self.assertEqual(subclass.encode('ascii'), b'ascii')
756001a37949 Optimize unicode_subtype_new(): don't encode to wchar_t and decode from wchar_t
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72558
diff changeset
1203 self.assertEqual(subclass.encode('utf-8'), b'ascii')
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1204
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1205 self.assertEqual(
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1206 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
1207 '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
1208 )
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 18828
diff changeset
1209
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1210 class StringCompat:
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1211 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
1212 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
1213 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
1214 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
1215
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1216 self.assertEqual(
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1217 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
1218 '__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
1219 )
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1220
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1221 # 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
1222
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1223 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
1224 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
1225 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
1226
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1227 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
1228 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
1229
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1230 # 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
1231 # 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
1232
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1233 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
1234 self.assertRaises(
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1235 TypeError,
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1236 str,
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1237 '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
1238 'utf-8',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1239 'strict'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1240 )
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
1241
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1242 self.assertEqual(
41620
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1243 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
1244 '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
1245 )
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
1246
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1247 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
1248 self.assertEqual(
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1249 str(
43487
8c10aac37037 Breaking ground for PEP 3137 implementation:
Guido van Rossum <guido@python.org>
parents: 43446
diff changeset
1250 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
1251 'utf-8',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1252 'strict'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1253 ),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1254 '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
1255 )
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1256
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1257 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
1258
80542
325f80d792b9 Improve str() and object.__str__() documentation (issue #13538).
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 80541
diff changeset
1259 def test_constructor_keyword_args(self):
325f80d792b9 Improve str() and object.__str__() documentation (issue #13538).
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 80541
diff changeset
1260 """Pass various keyword argument combinations to the constructor."""
325f80d792b9 Improve str() and object.__str__() documentation (issue #13538).
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 80541
diff changeset
1261 # The object argument can be passed as a keyword.
325f80d792b9 Improve str() and object.__str__() documentation (issue #13538).
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 80541
diff changeset
1262 self.assertEqual(str(object='foo'), 'foo')
325f80d792b9 Improve str() and object.__str__() documentation (issue #13538).
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 80541
diff changeset
1263 self.assertEqual(str(object=b'foo', encoding='utf-8'), 'foo')
325f80d792b9 Improve str() and object.__str__() documentation (issue #13538).
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 80541
diff changeset
1264 # The errors argument without encoding triggers "decode" mode.
325f80d792b9 Improve str() and object.__str__() documentation (issue #13538).
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 80541
diff changeset
1265 self.assertEqual(str(b'foo', errors='strict'), 'foo') # not "b'foo'"
325f80d792b9 Improve str() and object.__str__() documentation (issue #13538).
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 80541
diff changeset
1266 self.assertEqual(str(object=b'foo', errors='strict'), 'foo')
325f80d792b9 Improve str() and object.__str__() documentation (issue #13538).
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 80541
diff changeset
1267
325f80d792b9 Improve str() and object.__str__() documentation (issue #13538).
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 80541
diff changeset
1268 def test_constructor_defaults(self):
325f80d792b9 Improve str() and object.__str__() documentation (issue #13538).
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 80541
diff changeset
1269 """Check the constructor argument defaults."""
325f80d792b9 Improve str() and object.__str__() documentation (issue #13538).
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 80541
diff changeset
1270 # The object argument defaults to '' or b''.
325f80d792b9 Improve str() and object.__str__() documentation (issue #13538).
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 80541
diff changeset
1271 self.assertEqual(str(), '')
325f80d792b9 Improve str() and object.__str__() documentation (issue #13538).
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 80541
diff changeset
1272 self.assertEqual(str(errors='strict'), '')
325f80d792b9 Improve str() and object.__str__() documentation (issue #13538).
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 80541
diff changeset
1273 utf8_cent = '¢'.encode('utf-8')
325f80d792b9 Improve str() and object.__str__() documentation (issue #13538).
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 80541
diff changeset
1274 # The encoding argument defaults to utf-8.
325f80d792b9 Improve str() and object.__str__() documentation (issue #13538).
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 80541
diff changeset
1275 self.assertEqual(str(utf8_cent, errors='strict'), '¢')
325f80d792b9 Improve str() and object.__str__() documentation (issue #13538).
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 80541
diff changeset
1276 # The errors argument defaults to strict.
325f80d792b9 Improve str() and object.__str__() documentation (issue #13538).
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 80541
diff changeset
1277 self.assertRaises(UnicodeDecodeError, str, utf8_cent, encoding='ascii')
325f80d792b9 Improve str() and object.__str__() documentation (issue #13538).
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 80541
diff changeset
1278
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1279 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
1280 utfTests = [
41620
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1281 ('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
1282 ('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
1283 ('\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
1284 ('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
1285 ('+', b'+-'),
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1286 ('+-', b'+--'),
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1287 ('+?', b'+-?'),
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1288 ('\?', b'+AFw?'),
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1289 ('+?', b'+-?'),
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1290 (r'\\?', b'+AFwAXA?'),
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1291 (r'\\\?', b'+AFwAXABc?'),
53742
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1292 (r'++--', b'+-+---'),
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1293 ('\U000abcde', b'+2m/c3g-'), # surrogate pairs
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1294 ('/', b'/'),
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1295 ]
12051
d89556c04ef9 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
1296
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1297 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
1298 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
1299
73552
ddfcb0de564f Issue #13333: The UTF-7 decoder now accepts lone surrogates
Antoine Pitrou <solipsis@pitrou.net>
parents: 72813
diff changeset
1300 # 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
1301 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
1302 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
1303 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
1304 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
1305 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
1306 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
1307 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
1308 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
1309
73552
ddfcb0de564f Issue #13333: The UTF-7 decoder now accepts lone surrogates
Antoine Pitrou <solipsis@pitrou.net>
parents: 72813
diff changeset
1310 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
1311 self.assertEqual(b'+2AHab9ze-'.decode('utf-7'), '\uD801\U000abcde')
12331
d195f17f2927 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
1312
48570
862c0eab1c50 Merged revisions 65227 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48472
diff changeset
1313 # 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
1314 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
1315
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1316 # Direct encoded characters
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1317 set_d = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'(),-./:?"
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1318 # Optional direct characters
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1319 set_o = '!"#$%&*;<=>@[]^_`{|}'
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1320 for c in set_d:
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1321 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
1322 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
1323 for c in set_o:
9b9590007e82 Merged revisions 72283-72284 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53681
diff changeset
1324 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
1325
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1326 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
1327 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
1328 self.assertEqual('\u20ac'.encode('utf-8'), b'\xe2\x82\xac')
72666
f39b26ca7f3d #13054: fix usage of sys.maxunicode after PEP-393.
Ezio Melotti
parents: 72663
diff changeset
1329 self.assertEqual('\U00010002'.encode('utf-8'), b'\xf0\x90\x80\x82')
f39b26ca7f3d #13054: fix usage of sys.maxunicode after PEP-393.
Ezio Melotti
parents: 72663
diff changeset
1330 self.assertEqual('\U00023456'.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
1331 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
1332 self.assertEqual('\udc00'.encode('utf-8', 'surrogatepass'), b'\xed\xb0\x80')
72666
f39b26ca7f3d #13054: fix usage of sys.maxunicode after PEP-393.
Ezio Melotti
parents: 72663
diff changeset
1333 self.assertEqual(('\U00010002'*10).encode('utf-8'),
f39b26ca7f3d #13054: fix usage of sys.maxunicode after PEP-393.
Ezio Melotti
parents: 72663
diff changeset
1334 b'\xf0\x90\x80\x82'*10)
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1335 self.assertEqual(
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1336 '\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
1337 '\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
1338 '\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
1339 '\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
1340 '\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
1341 ' 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
1342 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
1343 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
1344 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
1345 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
1346 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
1347 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
1348 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
1349 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
1350 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
1351 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
1352 )
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
1353
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1354 # 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
1355 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
1356 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
1357 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
1358
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1359 # 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
1360 # * 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
1361 # UTF8_ERROR cases in PyUnicode_DecodeUTF8
12051
d89556c04ef9 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
1362
62710
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1363 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
1364 sequences = [
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1365 # single byte
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1366 (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
1367 # 2 bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1368 (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
1369 # 3 bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1370 (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
1371 (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
1372 # 4 bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1373 (b'\xF0\x90\x80\x80', '\U00010000'),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1374 (b'\xf4\x8f\xbf\xbf', '\U0010FFFF')
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1375 ]
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1376 for seq, res in sequences:
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1377 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
1378
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1379
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1380 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
1381 # 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
1382 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
1383 # 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
1384 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
1385 # 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
1386 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
1387 invalid_start_bytes = (
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1388 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
1389 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
1390 )
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1391
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1392 for byte in invalid_start_bytes:
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1393 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
1394
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1395 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
1396 for cb in continuation_bytes:
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1397 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
1398
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1399 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
1400 for cb1 in continuation_bytes[:3]:
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1401 for cb3 in continuation_bytes[:3]:
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1402 self.assertRaises(UnicodeDecodeError,
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1403 (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
1404
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1405 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
1406 self.assertRaises(UnicodeDecodeError,
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1407 (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
1408 self.assertRaises(UnicodeDecodeError,
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1409 (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
1410 # surrogates
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1411 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
1412 self.assertRaises(UnicodeDecodeError,
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1413 (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
1414 self.assertRaises(UnicodeDecodeError,
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1415 (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
1416 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
1417 self.assertRaises(UnicodeDecodeError,
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1418 (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
1419 self.assertRaises(UnicodeDecodeError,
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1420 (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
1421 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
1422 self.assertRaises(UnicodeDecodeError,
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1423 (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
1424 self.assertRaises(UnicodeDecodeError,
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1425 (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
1426
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1427 def test_issue8271(self):
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1428 # 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
1429 # 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
1430 # 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
1431 # 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
1432 # 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
1433 FFFD = '\ufffd'
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1434 sequences = [
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1435 # invalid start bytes
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1436 (b'\x80', FFFD), # continuation byte
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1437 (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
1438 (b'\xc0', FFFD),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1439 (b'\xc0\xc0', FFFD*2),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1440 (b'\xc1', FFFD),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1441 (b'\xc1\xc0', FFFD*2),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1442 (b'\xc0\xc1', FFFD*2),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1443 # 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
1444 (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
1445 (b'\xc2\xc2', FFFD*2), # 2 start bytes
80246
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1446 (b'\xc2\xc2\xc2', FFFD*3), # 3 start bytes
62710
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1447 (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
1448 # 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
1449 (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
1450 (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
1451 (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
1452 (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
1453 (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
1454 (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
1455 (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
1456 (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
1457 (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
1458 (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
1459 (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
1460 # 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
1461 (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
1462 (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
1463 (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
1464 (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
1465 (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
1466 (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
1467 (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
1468 (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
1469 (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
1470 (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
1471 (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
1472 (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
1473 (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
1474 (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
1475 (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
1476 (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
1477 (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
1478 (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
1479 # 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
1480 (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
1481 (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
1482 (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
1483 (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
1484 (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
1485 (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
1486 (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
1487 (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
1488 # 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
1489 (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
1490 (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
1491 (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
1492 (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
1493 (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
1494 # 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
1495 (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
1496 (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
1497 (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
1498 (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
1499 # invalid start byte
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1500 (b'\xfe', FFFD),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1501 (b'\xfe\x80\x80', FFFD*3),
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1502 # other sequences
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1503 (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
1504 (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
1505 (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
1506 (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
1507 '\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
1508 ]
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1509 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
1510 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
1511 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
1512 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
1513 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
1514 res.replace('\uFFFD', ''))
9dc5f385f494 Merged revisions 82413,82468 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62185
diff changeset
1515
80246
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1516 def to_bytestring(self, seq):
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1517 return bytes(int(c, 16) for c in seq.split())
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1518
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1519 def assertCorrectUTF8Decoding(self, seq, res, err):
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1520 """
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1521 Check that an invalid UTF-8 sequence raises an UnicodeDecodeError when
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1522 'strict' is used, returns res when 'replace' is used, and that doesn't
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1523 return anything when 'ignore' is used.
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1524 """
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1525 with self.assertRaises(UnicodeDecodeError) as cm:
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1526 seq.decode('utf-8')
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1527 exc = cm.exception
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1528
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1529 self.assertIn(err, str(exc))
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1530 self.assertEqual(seq.decode('utf-8', 'replace'), res)
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1531 self.assertEqual((b'aaaa' + seq + b'bbbb').decode('utf-8', 'replace'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1532 'aaaa' + res + 'bbbb')
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1533 res = res.replace('\ufffd', '')
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1534 self.assertEqual(seq.decode('utf-8', 'ignore'), res)
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1535 self.assertEqual((b'aaaa' + seq + b'bbbb').decode('utf-8', 'ignore'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1536 'aaaa' + res + 'bbbb')
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1537
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1538 def test_invalid_start_byte(self):
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1539 """
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1540 Test that an 'invalid start byte' error is raised when the first byte
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1541 is not in the ASCII range or is not a valid start byte of a 2-, 3-, or
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1542 4-bytes sequence. The invalid start byte is replaced with a single
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1543 U+FFFD when errors='replace'.
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1544 E.g. <80> is a continuation byte and can appear only after a start byte.
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1545 """
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1546 FFFD = '\ufffd'
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1547 for byte in b'\x80\xA0\x9F\xBF\xC0\xC1\xF5\xFF':
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1548 self.assertCorrectUTF8Decoding(bytes([byte]), '\ufffd',
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1549 'invalid start byte')
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1550
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1551 def test_unexpected_end_of_data(self):
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1552 """
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1553 Test that an 'unexpected end of data' error is raised when the string
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1554 ends after a start byte of a 2-, 3-, or 4-bytes sequence without having
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1555 enough continuation bytes. The incomplete sequence is replaced with a
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1556 single U+FFFD when errors='replace'.
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1557 E.g. in the sequence <F3 80 80>, F3 is the start byte of a 4-bytes
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1558 sequence, but it's followed by only 2 valid continuation bytes and the
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1559 last continuation bytes is missing.
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1560 Note: the continuation bytes must be all valid, if one of them is
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1561 invalid another error will be raised.
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1562 """
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1563 sequences = [
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1564 'C2', 'DF',
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1565 'E0 A0', 'E0 BF', 'E1 80', 'E1 BF', 'EC 80', 'EC BF',
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1566 'ED 80', 'ED 9F', 'EE 80', 'EE BF', 'EF 80', 'EF BF',
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1567 'F0 90', 'F0 BF', 'F0 90 80', 'F0 90 BF', 'F0 BF 80', 'F0 BF BF',
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1568 'F1 80', 'F1 BF', 'F1 80 80', 'F1 80 BF', 'F1 BF 80', 'F1 BF BF',
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1569 'F3 80', 'F3 BF', 'F3 80 80', 'F3 80 BF', 'F3 BF 80', 'F3 BF BF',
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1570 'F4 80', 'F4 8F', 'F4 80 80', 'F4 80 BF', 'F4 8F 80', 'F4 8F BF'
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1571 ]
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1572 FFFD = '\ufffd'
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1573 for seq in sequences:
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1574 self.assertCorrectUTF8Decoding(self.to_bytestring(seq), '\ufffd',
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1575 'unexpected end of data')
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1576
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1577 def test_invalid_cb_for_2bytes_seq(self):
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1578 """
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1579 Test that an 'invalid continuation byte' error is raised when the
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1580 continuation byte of a 2-bytes sequence is invalid. The start byte
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1581 is replaced by a single U+FFFD and the second byte is handled
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1582 separately when errors='replace'.
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1583 E.g. in the sequence <C2 41>, C2 is the start byte of a 2-bytes
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1584 sequence, but 41 is not a valid continuation byte because it's the
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1585 ASCII letter 'A'.
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1586 """
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1587 FFFD = '\ufffd'
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1588 FFFDx2 = FFFD * 2
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1589 sequences = [
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1590 ('C2 00', FFFD+'\x00'), ('C2 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1591 ('C2 C0', FFFDx2), ('C2 FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1592 ('DF 00', FFFD+'\x00'), ('DF 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1593 ('DF C0', FFFDx2), ('DF FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1594 ]
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1595 for seq, res in sequences:
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1596 self.assertCorrectUTF8Decoding(self.to_bytestring(seq), res,
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1597 'invalid continuation byte')
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1598
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1599 def test_invalid_cb_for_3bytes_seq(self):
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1600 """
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1601 Test that an 'invalid continuation byte' error is raised when the
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1602 continuation byte(s) of a 3-bytes sequence are invalid. When
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1603 errors='replace', if the first continuation byte is valid, the first
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1604 two bytes (start byte + 1st cb) are replaced by a single U+FFFD and the
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1605 third byte is handled separately, otherwise only the start byte is
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1606 replaced with a U+FFFD and the other continuation bytes are handled
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1607 separately.
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1608 E.g. in the sequence <E1 80 41>, E1 is the start byte of a 3-bytes
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1609 sequence, 80 is a valid continuation byte, but 41 is not a valid cb
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1610 because it's the ASCII letter 'A'.
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1611 Note: when the start byte is E0 or ED, the valid ranges for the first
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1612 continuation byte are limited to A0..BF and 80..9F respectively.
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1613 Python 2 used to consider all the bytes in range 80..BF valid when the
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1614 start byte was ED. This is fixed in Python 3.
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1615 """
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1616 FFFD = '\ufffd'
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1617 FFFDx2 = FFFD * 2
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1618 sequences = [
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1619 ('E0 00', FFFD+'\x00'), ('E0 7F', FFFD+'\x7f'), ('E0 80', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1620 ('E0 9F', FFFDx2), ('E0 C0', FFFDx2), ('E0 FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1621 ('E0 A0 00', FFFD+'\x00'), ('E0 A0 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1622 ('E0 A0 C0', FFFDx2), ('E0 A0 FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1623 ('E0 BF 00', FFFD+'\x00'), ('E0 BF 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1624 ('E0 BF C0', FFFDx2), ('E0 BF FF', FFFDx2), ('E1 00', FFFD+'\x00'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1625 ('E1 7F', FFFD+'\x7f'), ('E1 C0', FFFDx2), ('E1 FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1626 ('E1 80 00', FFFD+'\x00'), ('E1 80 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1627 ('E1 80 C0', FFFDx2), ('E1 80 FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1628 ('E1 BF 00', FFFD+'\x00'), ('E1 BF 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1629 ('E1 BF C0', FFFDx2), ('E1 BF FF', FFFDx2), ('EC 00', FFFD+'\x00'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1630 ('EC 7F', FFFD+'\x7f'), ('EC C0', FFFDx2), ('EC FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1631 ('EC 80 00', FFFD+'\x00'), ('EC 80 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1632 ('EC 80 C0', FFFDx2), ('EC 80 FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1633 ('EC BF 00', FFFD+'\x00'), ('EC BF 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1634 ('EC BF C0', FFFDx2), ('EC BF FF', FFFDx2), ('ED 00', FFFD+'\x00'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1635 ('ED 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1636 ('ED A0', FFFDx2), ('ED BF', FFFDx2), # see note ^
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1637 ('ED C0', FFFDx2), ('ED FF', FFFDx2), ('ED 80 00', FFFD+'\x00'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1638 ('ED 80 7F', FFFD+'\x7f'), ('ED 80 C0', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1639 ('ED 80 FF', FFFDx2), ('ED 9F 00', FFFD+'\x00'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1640 ('ED 9F 7F', FFFD+'\x7f'), ('ED 9F C0', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1641 ('ED 9F FF', FFFDx2), ('EE 00', FFFD+'\x00'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1642 ('EE 7F', FFFD+'\x7f'), ('EE C0', FFFDx2), ('EE FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1643 ('EE 80 00', FFFD+'\x00'), ('EE 80 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1644 ('EE 80 C0', FFFDx2), ('EE 80 FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1645 ('EE BF 00', FFFD+'\x00'), ('EE BF 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1646 ('EE BF C0', FFFDx2), ('EE BF FF', FFFDx2), ('EF 00', FFFD+'\x00'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1647 ('EF 7F', FFFD+'\x7f'), ('EF C0', FFFDx2), ('EF FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1648 ('EF 80 00', FFFD+'\x00'), ('EF 80 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1649 ('EF 80 C0', FFFDx2), ('EF 80 FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1650 ('EF BF 00', FFFD+'\x00'), ('EF BF 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1651 ('EF BF C0', FFFDx2), ('EF BF FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1652 ]
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1653 for seq, res in sequences:
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1654 self.assertCorrectUTF8Decoding(self.to_bytestring(seq), res,
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1655 'invalid continuation byte')
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1656
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1657 def test_invalid_cb_for_4bytes_seq(self):
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1658 """
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1659 Test that an 'invalid continuation byte' error is raised when the
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1660 continuation byte(s) of a 4-bytes sequence are invalid. When
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1661 errors='replace',the start byte and all the following valid
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1662 continuation bytes are replaced with a single U+FFFD, and all the bytes
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1663 starting from the first invalid continuation bytes (included) are
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1664 handled separately.
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1665 E.g. in the sequence <E1 80 41>, E1 is the start byte of a 3-bytes
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1666 sequence, 80 is a valid continuation byte, but 41 is not a valid cb
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1667 because it's the ASCII letter 'A'.
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1668 Note: when the start byte is E0 or ED, the valid ranges for the first
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1669 continuation byte are limited to A0..BF and 80..9F respectively.
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1670 However, when the start byte is ED, Python 2 considers all the bytes
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1671 in range 80..BF valid. This is fixed in Python 3.
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1672 """
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1673 FFFD = '\ufffd'
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1674 FFFDx2 = FFFD * 2
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1675 sequences = [
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1676 ('F0 00', FFFD+'\x00'), ('F0 7F', FFFD+'\x7f'), ('F0 80', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1677 ('F0 8F', FFFDx2), ('F0 C0', FFFDx2), ('F0 FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1678 ('F0 90 00', FFFD+'\x00'), ('F0 90 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1679 ('F0 90 C0', FFFDx2), ('F0 90 FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1680 ('F0 BF 00', FFFD+'\x00'), ('F0 BF 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1681 ('F0 BF C0', FFFDx2), ('F0 BF FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1682 ('F0 90 80 00', FFFD+'\x00'), ('F0 90 80 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1683 ('F0 90 80 C0', FFFDx2), ('F0 90 80 FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1684 ('F0 90 BF 00', FFFD+'\x00'), ('F0 90 BF 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1685 ('F0 90 BF C0', FFFDx2), ('F0 90 BF FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1686 ('F0 BF 80 00', FFFD+'\x00'), ('F0 BF 80 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1687 ('F0 BF 80 C0', FFFDx2), ('F0 BF 80 FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1688 ('F0 BF BF 00', FFFD+'\x00'), ('F0 BF BF 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1689 ('F0 BF BF C0', FFFDx2), ('F0 BF BF FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1690 ('F1 00', FFFD+'\x00'), ('F1 7F', FFFD+'\x7f'), ('F1 C0', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1691 ('F1 FF', FFFDx2), ('F1 80 00', FFFD+'\x00'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1692 ('F1 80 7F', FFFD+'\x7f'), ('F1 80 C0', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1693 ('F1 80 FF', FFFDx2), ('F1 BF 00', FFFD+'\x00'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1694 ('F1 BF 7F', FFFD+'\x7f'), ('F1 BF C0', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1695 ('F1 BF FF', FFFDx2), ('F1 80 80 00', FFFD+'\x00'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1696 ('F1 80 80 7F', FFFD+'\x7f'), ('F1 80 80 C0', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1697 ('F1 80 80 FF', FFFDx2), ('F1 80 BF 00', FFFD+'\x00'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1698 ('F1 80 BF 7F', FFFD+'\x7f'), ('F1 80 BF C0', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1699 ('F1 80 BF FF', FFFDx2), ('F1 BF 80 00', FFFD+'\x00'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1700 ('F1 BF 80 7F', FFFD+'\x7f'), ('F1 BF 80 C0', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1701 ('F1 BF 80 FF', FFFDx2), ('F1 BF BF 00', FFFD+'\x00'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1702 ('F1 BF BF 7F', FFFD+'\x7f'), ('F1 BF BF C0', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1703 ('F1 BF BF FF', FFFDx2), ('F3 00', FFFD+'\x00'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1704 ('F3 7F', FFFD+'\x7f'), ('F3 C0', FFFDx2), ('F3 FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1705 ('F3 80 00', FFFD+'\x00'), ('F3 80 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1706 ('F3 80 C0', FFFDx2), ('F3 80 FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1707 ('F3 BF 00', FFFD+'\x00'), ('F3 BF 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1708 ('F3 BF C0', FFFDx2), ('F3 BF FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1709 ('F3 80 80 00', FFFD+'\x00'), ('F3 80 80 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1710 ('F3 80 80 C0', FFFDx2), ('F3 80 80 FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1711 ('F3 80 BF 00', FFFD+'\x00'), ('F3 80 BF 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1712 ('F3 80 BF C0', FFFDx2), ('F3 80 BF FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1713 ('F3 BF 80 00', FFFD+'\x00'), ('F3 BF 80 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1714 ('F3 BF 80 C0', FFFDx2), ('F3 BF 80 FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1715 ('F3 BF BF 00', FFFD+'\x00'), ('F3 BF BF 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1716 ('F3 BF BF C0', FFFDx2), ('F3 BF BF FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1717 ('F4 00', FFFD+'\x00'), ('F4 7F', FFFD+'\x7f'), ('F4 90', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1718 ('F4 BF', FFFDx2), ('F4 C0', FFFDx2), ('F4 FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1719 ('F4 80 00', FFFD+'\x00'), ('F4 80 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1720 ('F4 80 C0', FFFDx2), ('F4 80 FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1721 ('F4 8F 00', FFFD+'\x00'), ('F4 8F 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1722 ('F4 8F C0', FFFDx2), ('F4 8F FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1723 ('F4 80 80 00', FFFD+'\x00'), ('F4 80 80 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1724 ('F4 80 80 C0', FFFDx2), ('F4 80 80 FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1725 ('F4 80 BF 00', FFFD+'\x00'), ('F4 80 BF 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1726 ('F4 80 BF C0', FFFDx2), ('F4 80 BF FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1727 ('F4 8F 80 00', FFFD+'\x00'), ('F4 8F 80 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1728 ('F4 8F 80 C0', FFFDx2), ('F4 8F 80 FF', FFFDx2),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1729 ('F4 8F BF 00', FFFD+'\x00'), ('F4 8F BF 7F', FFFD+'\x7f'),
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1730 ('F4 8F BF C0', FFFDx2), ('F4 8F BF FF', FFFDx2)
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1731 ]
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1732 for seq, res in sequences:
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1733 self.assertCorrectUTF8Decoding(self.to_bytestring(seq), res,
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1734 'invalid continuation byte')
5962f192a483 #8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80016
diff changeset
1735
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
1736 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
1737 # 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
1738 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
1739
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1740 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
1741 # 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
1742 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
1743 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
1744 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
1745 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
1746 self.assertEqual('Andr\202 x'.encode('ascii', 'replace'),
1b7bdae02179 Merged revisions 74929 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
1747 'Andr\202 x'.encode('ascii', errors='replace'))
1b7bdae02179 Merged revisions 74929 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
1748 self.assertEqual('Andr\202 x'.encode('ascii', 'ignore'),
1b7bdae02179 Merged revisions 74929 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
1749 'Andr\202 x'.encode(encoding='ascii', errors='ignore'))
12051
d89556c04ef9 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
1750
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1751 # Error handling (decoding)
41620
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1752 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
1753 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
1754 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
1755 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
1756
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1757 # 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
1758 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
1759
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1760 # 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
1761 self.assertRaises(UnicodeError, b"\\".decode, "unicode-escape")
12051
d89556c04ef9 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
1762
42907
0170418dadea Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42902
diff changeset
1763 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
1764 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
1765 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
1766 self.assertRaises(TypeError, "hello".encode, "test.unicode2")
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
1767
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1768 # 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
1769 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
1770
66673
7b6af56ee74d Issue #10557: Fixed error messages from float() and other numeric
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66581
diff changeset
1771 # 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
1772 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
1773 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
1774 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
1775 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
1776
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1777 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
1778 # Encoding
41620
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1779 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
1780 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
1781 self.assertEqual('hello'.encode('utf-8'), b'hello')
68142
be6c38d1817b Normalize the encoding names for Latin-1 and UTF-8 to
Marc-André Lemburg <mal@egenix.com>
parents: 68087
diff changeset
1782 self.assertEqual('hello'.encode('utf-8'), b'hello')
41620
6b7d1782a574 test_unicode.py passes again 9except for problems
Walter Dörwald <walter@livinglogic.de>
parents: 41592
diff changeset
1783 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
1784 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
1785 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
1786
68142
be6c38d1817b Normalize the encoding names for Latin-1 and UTF-8 to
Marc-André Lemburg <mal@egenix.com>
parents: 68087
diff changeset
1787 # Default encoding is utf-8
be6c38d1817b Normalize the encoding names for Latin-1 and UTF-8 to
Marc-André Lemburg <mal@egenix.com>
parents: 68087
diff changeset
1788 self.assertEqual('\u2603'.encode(), b'\xe2\x98\x83')
be6c38d1817b Normalize the encoding names for Latin-1 and UTF-8 to
Marc-André Lemburg <mal@egenix.com>
parents: 68087
diff changeset
1789
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1790 # 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
1791 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
1792 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
1793 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
1794 '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
1795 'unicode_escape', 'unicode_internal'):
73579
ec8c0ac81706 "unicode_internal" codec has been deprecated: fix related tests
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73553
diff changeset
1796 with warnings.catch_warnings():
ec8c0ac81706 "unicode_internal" codec has been deprecated: fix related tests
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73553
diff changeset
1797 # unicode-internal has been deprecated
ec8c0ac81706 "unicode_internal" codec has been deprecated: fix related tests
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73553
diff changeset
1798 warnings.simplefilter("ignore", DeprecationWarning)
ec8c0ac81706 "unicode_internal" codec has been deprecated: fix related tests
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73553
diff changeset
1799
ec8c0ac81706 "unicode_internal" codec has been deprecated: fix related tests
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73553
diff changeset
1800 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
1801
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1802 # 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
1803 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
1804 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
1805 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
1806 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
1807
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1808 # 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
1809 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
1810 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
1811 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
1812 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
1813
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1814 # Roundtrip safety for non-BMP (just a few chars)
73579
ec8c0ac81706 "unicode_internal" codec has been deprecated: fix related tests
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73553
diff changeset
1815 with warnings.catch_warnings():
ec8c0ac81706 "unicode_internal" codec has been deprecated: fix related tests
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73553
diff changeset
1816 # unicode-internal has been deprecated
ec8c0ac81706 "unicode_internal" codec has been deprecated: fix related tests
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73553
diff changeset
1817 warnings.simplefilter("ignore", DeprecationWarning)
17815
d6df8d371b67 Get rid of the superstitious "~" in dict hashing's "i = (~hash) & mask".
Tim Peters <tim.peters@gmail.com>
parents: 17655
diff changeset
1818
73579
ec8c0ac81706 "unicode_internal" codec has been deprecated: fix related tests
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73553
diff changeset
1819 u = '\U00010001\U00020002\U00030003\U00040004\U00050005'
ec8c0ac81706 "unicode_internal" codec has been deprecated: fix related tests
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73553
diff changeset
1820 for encoding in ('utf-8', 'utf-16', 'utf-16-le', 'utf-16-be',
ec8c0ac81706 "unicode_internal" codec has been deprecated: fix related tests
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73553
diff changeset
1821 'raw_unicode_escape',
ec8c0ac81706 "unicode_internal" codec has been deprecated: fix related tests
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73553
diff changeset
1822 'unicode_escape', 'unicode_internal'):
ec8c0ac81706 "unicode_internal" codec has been deprecated: fix related tests
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73553
diff changeset
1823 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
1824
73497
bd5c4dda6ad0 Make test more inclusive
Antoine Pitrou <solipsis@pitrou.net>
parents: 73496
diff changeset
1825 # UTF-8 must be roundtrip safe for all code points
bd5c4dda6ad0 Make test more inclusive
Antoine Pitrou <solipsis@pitrou.net>
parents: 73496
diff changeset
1826 # (except surrogates, which are forbidden).
bd5c4dda6ad0 Make test more inclusive
Antoine Pitrou <solipsis@pitrou.net>
parents: 73496
diff changeset
1827 u = ''.join(map(chr, list(range(0, 0xd800)) +
73502
ed0944c5e2d5 Fix range in test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73497
diff changeset
1828 list(range(0xe000, 0x110000))))
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1829 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
1830 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
1831
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1832 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
1833 # 0-127
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41620
diff changeset
1834 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
1835 for encoding in (
87032
7d9d1bcd7d18 #1097797: Add CP273 codec, and exercise it in the test suite
Andrew Kuchling <amk@amk.ca>
parents: 85475
diff changeset
1836 'cp037', 'cp1026', 'cp273',
62572
1d1797af4622 Merged revisions 81499,81506 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62171
diff changeset
1837 'cp437', 'cp500', 'cp720', 'cp737', 'cp775', 'cp850',
1d1797af4622 Merged revisions 81499,81506 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62171
diff changeset
1838 'cp852', 'cp855', 'cp858', 'cp860', 'cp861', 'cp862',
87430
d0fd68ef1aa9 Issue #19668: Added support for the cp1125 encoding.
Serhiy Storchaka <storchaka@gmail.com>
parents: 87032
diff changeset
1839 'cp863', 'cp865', 'cp866', 'cp1125',
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1840 '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
1841 '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
1842 '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
1843 '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
1844
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1845 '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
1846 '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
1847 '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
1848
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1849 '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
1850 '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
1851
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1852 ### 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
1853 #'cp424',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1854
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1855 ### 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
1856 #'cp875'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1857
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1858 ):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1859 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
1860
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1861 # 128-255
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41620
diff changeset
1862 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
1863 for encoding in (
87032
7d9d1bcd7d18 #1097797: Add CP273 codec, and exercise it in the test suite
Andrew Kuchling <amk@amk.ca>
parents: 85475
diff changeset
1864 'cp037', 'cp1026', 'cp273',
62572
1d1797af4622 Merged revisions 81499,81506 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62171
diff changeset
1865 'cp437', 'cp500', 'cp720', 'cp737', 'cp775', 'cp850',
1d1797af4622 Merged revisions 81499,81506 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62171
diff changeset
1866 'cp852', 'cp855', 'cp858', 'cp860', 'cp861', 'cp862',
87430
d0fd68ef1aa9 Issue #19668: Added support for the cp1125 encoding.
Serhiy Storchaka <storchaka@gmail.com>
parents: 87032
diff changeset
1867 'cp863', 'cp865', 'cp866', 'cp1125',
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1868 '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
1869 '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
1870 '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
1871 '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
1872
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1873 ### 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
1874 #'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
1875 #'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
1876 #'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
1877 #'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
1878 #'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
1879
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1880 ### 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
1881 #'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
1882
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1883 ):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1884 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
1885
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1886 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
1887 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
1888 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
1889 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
1890 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
1891 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
1892
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1893 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
1894 class BitBucket:
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1895 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
1896 pass
24598
12e146171f3d Committing patch #591250 which provides "str1 in str2" when str1 is a
Barry Warsaw <barry@python.org>
parents: 24538
diff changeset
1897
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1898 out = BitBucket()
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1899 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
1900 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
1901 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
1902 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
1903 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
1904 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
1905 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
1906 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
1907 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
1908
29164
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28917
diff changeset
1909 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
1910 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
1911 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
1912 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
1913
64694
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1914 y = br'\U00100000'
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1915 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
1916 self.assertEqual(x, y)
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1917 y = br'\U00010000'
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1918 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
1919 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
1920
64694
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1921 try:
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1922 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
1923 except UnicodeDecodeError as e:
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1924 self.assertEqual(e.start, 0)
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1925 self.assertEqual(e.end, 10)
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1926 else:
1e89b5ee02f8 Reenable test_ucs4 and remove some duplicated lines.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64666
diff changeset
1927 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
1928
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1929 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
1930 # 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
1931 class Foo0:
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1932 def __str__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1933 return "foo"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1934
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1935 class Foo1:
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
1936 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
1937 return "foo"
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1938
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1939 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
1940 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
1941 return "foo"
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1942
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1943 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
1944 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
1945 return "foo"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1946
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1947 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
1948 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
1949 return "foo"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1950
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1951 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
1952 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
1953 return "foo"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1954
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1955 class Foo6(str):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1956 def __str__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1957 return "foos"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1958
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
1959 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
1960 return "foou"
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1961
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1962 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
1963 def __str__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1964 return "foos"
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
1965 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
1966 return "foou"
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1967
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1968 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
1969 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
1970 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
1971 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
1972 return self
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1973
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1974 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
1975 def __str__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1976 return "not unicode"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1977
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1978 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
1979 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
1980 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
1981 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
1982 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
1983 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
1984 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
1985 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
1986 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
1987 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
1988
37301
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1989 def test_unicode_repr(self):
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1990 class s1:
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1991 def __repr__(self):
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1992 return '\\n'
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1993
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1994 class s2:
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1995 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
1996 return '\\n'
37301
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1997
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1998 self.assertEqual(repr(s1()), '\\n')
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1999 self.assertEqual(repr(s2()), '\\n')
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
2000
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
2001 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
2002 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
2003 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
2004
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41702
diff changeset
2005 def test_expandtabs_overflows_gracefully(self):
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41702
diff changeset
2006 # 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
2007 # 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
2008 # 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
2009 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
2010 return
44160
9d152530910a Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents: 44039
diff changeset
2011 self.assertRaises(OverflowError, 't\tt\t'.expandtabs, sys.maxsize)
37301
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
2012
72788
e3d5b718f2e6 Mark 'abc'.expandtab() optimization as specific to CPython
Victor Stinner <vstinner@wyplay.com>
parents: 72784
diff changeset
2013 @support.cpython_only
72663
447f521ac6d9 When expandtabs() would be a no-op, don't create a duplicate string
Antoine Pitrou <solipsis@pitrou.net>
parents: 72566
diff changeset
2014 def test_expandtabs_optimization(self):
447f521ac6d9 When expandtabs() would be a no-op, don't create a duplicate string
Antoine Pitrou <solipsis@pitrou.net>
parents: 72566
diff changeset
2015 s = 'abc'
447f521ac6d9 When expandtabs() would be a no-op, don't create a duplicate string
Antoine Pitrou <solipsis@pitrou.net>
parents: 72566
diff changeset
2016 self.assertIs(s.expandtabs(), s)
447f521ac6d9 When expandtabs() would be a no-op, don't create a duplicate string
Antoine Pitrou <solipsis@pitrou.net>
parents: 72566
diff changeset
2017
48601
a476ef5eebad Merged revisions 65339-65340,65342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48570
diff changeset
2018 def test_raiseMemError(self):
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2019 if struct.calcsize('P') == 8:
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2020 # 64 bits pointers
72480
f19af08eef02 Fix struct sizes. Drop -1, since the resulting string was actually the largest one
Martin v. Löwis <martin@v.loewis.de>
parents: 72475
diff changeset
2021 ascii_struct_size = 48
f19af08eef02 Fix struct sizes. Drop -1, since the resulting string was actually the largest one
Martin v. Löwis <martin@v.loewis.de>
parents: 72475
diff changeset
2022 compact_struct_size = 72
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2023 else:
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2024 # 32 bits pointers
72480
f19af08eef02 Fix struct sizes. Drop -1, since the resulting string was actually the largest one
Martin v. Löwis <martin@v.loewis.de>
parents: 72475
diff changeset
2025 ascii_struct_size = 24
f19af08eef02 Fix struct sizes. Drop -1, since the resulting string was actually the largest one
Martin v. Löwis <martin@v.loewis.de>
parents: 72475
diff changeset
2026 compact_struct_size = 36
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2027
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2028 for char in ('a', '\xe9', '\u20ac', '\U0010ffff'):
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2029 code = ord(char)
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2030 if code < 0x100:
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2031 char_size = 1 # sizeof(Py_UCS1)
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2032 struct_size = ascii_struct_size
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2033 elif code < 0x10000:
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2034 char_size = 2 # sizeof(Py_UCS2)
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2035 struct_size = compact_struct_size
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2036 else:
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2037 char_size = 4 # sizeof(Py_UCS4)
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2038 struct_size = compact_struct_size
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2039 # Note: sys.maxsize is half of the actual max allocation because of
72480
f19af08eef02 Fix struct sizes. Drop -1, since the resulting string was actually the largest one
Martin v. Löwis <martin@v.loewis.de>
parents: 72475
diff changeset
2040 # the signedness of Py_ssize_t. Strings of maxlen-1 should in principle
f19af08eef02 Fix struct sizes. Drop -1, since the resulting string was actually the largest one
Martin v. Löwis <martin@v.loewis.de>
parents: 72475
diff changeset
2041 # be allocatable, given enough memory.
f19af08eef02 Fix struct sizes. Drop -1, since the resulting string was actually the largest one
Martin v. Löwis <martin@v.loewis.de>
parents: 72475
diff changeset
2042 maxlen = ((sys.maxsize - struct_size) // char_size)
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2043 alloc = lambda: char * maxlen
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2044 self.assertRaises(MemoryError, alloc)
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2045 self.assertRaises(MemoryError, alloc)
48723
1d88ad5f4def Merged revisions 65773 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48601
diff changeset
2046
59899
84405c22e4c7 Merged revisions 79281 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 55593
diff changeset
2047 def test_format_subclass(self):
84405c22e4c7 Merged revisions 79281 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 55593
diff changeset
2048 class S(str):
84405c22e4c7 Merged revisions 79281 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 55593
diff changeset
2049 def __str__(self):
84405c22e4c7 Merged revisions 79281 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 55593
diff changeset
2050 return '__str__ overridden'
84405c22e4c7 Merged revisions 79281 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 55593
diff changeset
2051 s = S('xxx')
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65661
diff changeset
2052 self.assertEqual("%s" % s, '__str__ overridden')
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65661
diff changeset
2053 self.assertEqual("{}".format(s), '__str__ overridden')
59899
84405c22e4c7 Merged revisions 79281 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 55593
diff changeset
2054
67363
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
2055 # 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
2056 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
2057 support.import_module('ctypes')
79545
e16ec3b468d1 Issue #16147: PyUnicode_FromFormatV() doesn't need anymore to allocate a buffer
Victor Stinner <victor.stinner@gmail.com>
parents: 79542
diff changeset
2058 from ctypes import (
e16ec3b468d1 Issue #16147: PyUnicode_FromFormatV() doesn't need anymore to allocate a buffer
Victor Stinner <victor.stinner@gmail.com>
parents: 79542
diff changeset
2059 pythonapi, py_object, sizeof,
68203
87de017da4bf Issue #10831: PyUnicode_FromFormat() supports %li, %lli and %zi formats
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68201
diff changeset
2060 c_int, c_long, c_longlong, c_ssize_t,
79545
e16ec3b468d1 Issue #16147: PyUnicode_FromFormatV() doesn't need anymore to allocate a buffer
Victor Stinner <victor.stinner@gmail.com>
parents: 79542
diff changeset
2061 c_uint, c_ulong, c_ulonglong, c_size_t, c_void_p)
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72041
diff changeset
2062 name = "PyUnicode_FromFormat"
67363
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
2063 _PyUnicode_FromFormat = getattr(pythonapi, name)
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
2064 _PyUnicode_FromFormat.restype = py_object
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
2065
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
2066 def PyUnicode_FromFormat(format, *args):
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
2067 cargs = tuple(
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
2068 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
2069 for arg in args)
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
2070 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
2071
83658
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2072 def check_format(expected, format, *args):
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2073 text = PyUnicode_FromFormat(format, *args)
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2074 self.assertEqual(expected, text)
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2075
64639
1cfad55de5bd Issue #9738: PyUnicode_FromFormat() and PyErr_Format() raise an error on
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64146
diff changeset
2076 # ascii format, non-ascii argument
83658
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2077 check_format('ascii\x7f=unicode\xe9',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2078 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
2079
67363
dd3f28ffd029 test_unicode: use ctypes to test PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66673
diff changeset
2080 # 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
2081 # raises an error
66581
38bec90fd8ab #10273: Rename assertRegexpMatches and assertRaisesRegexp to assertRegex and assertRaisesRegex.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66402
diff changeset
2082 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
2083 '^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
2084 '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
2085 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
2086
68201
da95440d92b3 Issue #10829: Refactor PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68198
diff changeset
2087 # test "%c"
83658
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2088 check_format('\uabcd',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2089 b'%c', c_int(0xabcd))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2090 check_format('\U0010ffff',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2091 b'%c', c_int(0x10ffff))
84281
f8ede55cf92b Issue #18184: PyUnicode_FromFormat() and PyUnicode_FromFormatV() now raise
Serhiy Storchaka <storchaka@gmail.com>
parents: 84100
diff changeset
2092 with self.assertRaises(OverflowError):
f8ede55cf92b Issue #18184: PyUnicode_FromFormat() and PyUnicode_FromFormatV() now raise
Serhiy Storchaka <storchaka@gmail.com>
parents: 84100
diff changeset
2093 PyUnicode_FromFormat(b'%c', c_int(0x110000))
84100
b11507395ce4 Add tests for issue #18183.
Serhiy Storchaka <storchaka@gmail.com>
parents: 84083
diff changeset
2094 # Issue #18183
84101
17c9f1627baf Add tests for issue #18183.
Serhiy Storchaka <storchaka@gmail.com>
parents: 84084 84100
diff changeset
2095 check_format('\U00010000\U00100000',
17c9f1627baf Add tests for issue #18183.
Serhiy Storchaka <storchaka@gmail.com>
parents: 84084 84100
diff changeset
2096 b'%c%c', c_int(0x10000), c_int(0x100000))
68087
ef28aad3bbae Fix PyUnicode_FromFormatV("%c") for non-BMP char
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67791
diff changeset
2097
68201
da95440d92b3 Issue #10829: Refactor PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68198
diff changeset
2098 # test "%"
83658
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2099 check_format('%',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2100 b'%')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2101 check_format('%',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2102 b'%%')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2103 check_format('%s',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2104 b'%%s')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2105 check_format('[%]',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2106 b'[%%]')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2107 check_format('%abc',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2108 b'%%%s', b'abc')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2109
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2110 # truncated string
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2111 check_format('abc',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2112 b'%.3s', b'abcdef')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2113 check_format('abc[\ufffd',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2114 b'%.5s', 'abc[\u20ac]'.encode('utf8'))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2115 check_format("'\\u20acABC'",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2116 b'%A', '\u20acABC')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2117 check_format("'\\u20",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2118 b'%.5A', '\u20acABCDEF')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2119 check_format("'\u20acABC'",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2120 b'%R', '\u20acABC')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2121 check_format("'\u20acA",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2122 b'%.3R', '\u20acABCDEF')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2123 check_format('\u20acAB',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2124 b'%.3S', '\u20acABCDEF')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2125 check_format('\u20acAB',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2126 b'%.3U', '\u20acABCDEF')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2127 check_format('\u20acAB',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2128 b'%.3V', '\u20acABCDEF', None)
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2129 check_format('abc[\ufffd',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2130 b'%.5V', None, 'abc[\u20ac]'.encode('utf8'))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2131
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2132 # following tests comes from #7330
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2133 # test width modifier and precision modifier with %S
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2134 check_format("repr= abc",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2135 b'repr=%5S', 'abc')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2136 check_format("repr=ab",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2137 b'repr=%.2S', 'abc')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2138 check_format("repr= ab",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2139 b'repr=%5.2S', 'abc')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2140
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2141 # test width modifier and precision modifier with %R
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2142 check_format("repr= 'abc'",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2143 b'repr=%8R', 'abc')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2144 check_format("repr='ab",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2145 b'repr=%.3R', 'abc')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2146 check_format("repr= 'ab",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2147 b'repr=%5.3R', 'abc')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2148
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2149 # test width modifier and precision modifier with %A
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2150 check_format("repr= 'abc'",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2151 b'repr=%8A', 'abc')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2152 check_format("repr='ab",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2153 b'repr=%.3A', 'abc')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2154 check_format("repr= 'ab",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2155 b'repr=%5.3A', 'abc')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2156
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2157 # test width modifier and precision modifier with %s
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2158 check_format("repr= abc",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2159 b'repr=%5s', b'abc')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2160 check_format("repr=ab",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2161 b'repr=%.2s', b'abc')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2162 check_format("repr= ab",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2163 b'repr=%5.2s', b'abc')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2164
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2165 # test width modifier and precision modifier with %U
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2166 check_format("repr= abc",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2167 b'repr=%5U', 'abc')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2168 check_format("repr=ab",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2169 b'repr=%.2U', 'abc')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2170 check_format("repr= ab",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2171 b'repr=%5.2U', 'abc')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2172
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2173 # test width modifier and precision modifier with %V
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2174 check_format("repr= abc",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2175 b'repr=%5V', 'abc', b'123')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2176 check_format("repr=ab",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2177 b'repr=%.2V', 'abc', b'123')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2178 check_format("repr= ab",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2179 b'repr=%5.2V', 'abc', b'123')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2180 check_format("repr= 123",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2181 b'repr=%5V', None, b'123')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2182 check_format("repr=12",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2183 b'repr=%.2V', None, b'123')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2184 check_format("repr= 12",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2185 b'repr=%5.2V', None, b'123')
68201
da95440d92b3 Issue #10829: Refactor PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68198
diff changeset
2186
68203
87de017da4bf Issue #10831: PyUnicode_FromFormat() supports %li, %lli and %zi formats
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68201
diff changeset
2187 # test integer formats (%i, %d, %u)
83658
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2188 check_format('010',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2189 b'%03i', c_int(10))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2190 check_format('0010',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2191 b'%0.4i', c_int(10))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2192 check_format('-123',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2193 b'%i', c_int(-123))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2194 check_format('-123',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2195 b'%li', c_long(-123))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2196 check_format('-123',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2197 b'%lli', c_longlong(-123))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2198 check_format('-123',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2199 b'%zi', c_ssize_t(-123))
68201
da95440d92b3 Issue #10829: Refactor PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68198
diff changeset
2200
83658
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2201 check_format('-123',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2202 b'%d', c_int(-123))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2203 check_format('-123',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2204 b'%ld', c_long(-123))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2205 check_format('-123',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2206 b'%lld', c_longlong(-123))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2207 check_format('-123',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2208 b'%zd', c_ssize_t(-123))
68201
da95440d92b3 Issue #10829: Refactor PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68198
diff changeset
2209
83658
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2210 check_format('123',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2211 b'%u', c_uint(123))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2212 check_format('123',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2213 b'%lu', c_ulong(123))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2214 check_format('123',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2215 b'%llu', c_ulonglong(123))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2216 check_format('123',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2217 b'%zu', c_size_t(123))
68201
da95440d92b3 Issue #10829: Refactor PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68198
diff changeset
2218
79545
e16ec3b468d1 Issue #16147: PyUnicode_FromFormatV() doesn't need anymore to allocate a buffer
Victor Stinner <victor.stinner@gmail.com>
parents: 79542
diff changeset
2219 # test long output
e16ec3b468d1 Issue #16147: PyUnicode_FromFormatV() doesn't need anymore to allocate a buffer
Victor Stinner <victor.stinner@gmail.com>
parents: 79542
diff changeset
2220 min_longlong = -(2 ** (8 * sizeof(c_longlong) - 1))
e16ec3b468d1 Issue #16147: PyUnicode_FromFormatV() doesn't need anymore to allocate a buffer
Victor Stinner <victor.stinner@gmail.com>
parents: 79542
diff changeset
2221 max_longlong = -min_longlong - 1
83658
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2222 check_format(str(min_longlong),
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2223 b'%lld', c_longlong(min_longlong))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2224 check_format(str(max_longlong),
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2225 b'%lld', c_longlong(max_longlong))
79545
e16ec3b468d1 Issue #16147: PyUnicode_FromFormatV() doesn't need anymore to allocate a buffer
Victor Stinner <victor.stinner@gmail.com>
parents: 79542
diff changeset
2226 max_ulonglong = 2 ** (8 * sizeof(c_ulonglong)) - 1
83658
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2227 check_format(str(max_ulonglong),
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2228 b'%llu', c_ulonglong(max_ulonglong))
79545
e16ec3b468d1 Issue #16147: PyUnicode_FromFormatV() doesn't need anymore to allocate a buffer
Victor Stinner <victor.stinner@gmail.com>
parents: 79542
diff changeset
2229 PyUnicode_FromFormat(b'%p', c_void_p(-1))
68201
da95440d92b3 Issue #10829: Refactor PyUnicode_FromFormat()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68198
diff changeset
2230
79542
b4bee17625e1 Issue #16147: Rewrite PyUnicode_FromFormatV() to use _PyUnicodeWriter API
Victor Stinner <victor.stinner@gmail.com>
parents: 78440
diff changeset
2231 # test padding (width and/or precision)
83658
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2232 check_format('123'.rjust(10, '0'),
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2233 b'%010i', c_int(123))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2234 check_format('123'.rjust(100),
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2235 b'%100i', c_int(123))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2236 check_format('123'.rjust(100, '0'),
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2237 b'%.100i', c_int(123))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2238 check_format('123'.rjust(80, '0').rjust(100),
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2239 b'%100.80i', c_int(123))
79542
b4bee17625e1 Issue #16147: Rewrite PyUnicode_FromFormatV() to use _PyUnicodeWriter API
Victor Stinner <victor.stinner@gmail.com>
parents: 78440
diff changeset
2240
83658
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2241 check_format('123'.rjust(10, '0'),
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2242 b'%010u', c_uint(123))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2243 check_format('123'.rjust(100),
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2244 b'%100u', c_uint(123))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2245 check_format('123'.rjust(100, '0'),
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2246 b'%.100u', c_uint(123))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2247 check_format('123'.rjust(80, '0').rjust(100),
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2248 b'%100.80u', c_uint(123))
79542
b4bee17625e1 Issue #16147: Rewrite PyUnicode_FromFormatV() to use _PyUnicodeWriter API
Victor Stinner <victor.stinner@gmail.com>
parents: 78440
diff changeset
2249
83658
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2250 check_format('123'.rjust(10, '0'),
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2251 b'%010x', c_int(0x123))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2252 check_format('123'.rjust(100),
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2253 b'%100x', c_int(0x123))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2254 check_format('123'.rjust(100, '0'),
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2255 b'%.100x', c_int(0x123))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2256 check_format('123'.rjust(80, '0').rjust(100),
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2257 b'%100.80x', c_int(0x123))
68203
87de017da4bf Issue #10831: PyUnicode_FromFormat() supports %li, %lli and %zi formats
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68201
diff changeset
2258
87de017da4bf Issue #10831: PyUnicode_FromFormat() supports %li, %lli and %zi formats
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68201
diff changeset
2259 # test %A
83658
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2260 check_format(r"%A:'abc\xe9\uabcd\U0010ffff'",
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2261 b'%%A:%A', 'abc\xe9\uabcd\U0010ffff')
65530
3faff7e51f40 PyUnicode_FromFormatV(): Fix %A format
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65217
diff changeset
2262
68203
87de017da4bf Issue #10831: PyUnicode_FromFormat() supports %li, %lli and %zi formats
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68201
diff changeset
2263 # test %V
83658
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2264 check_format('repr=abc',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2265 b'repr=%V', 'abc', b'xyz')
68058
72f30f8ef331 Merged revisions 88697 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68087
diff changeset
2266
72f30f8ef331 Merged revisions 88697 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68087
diff changeset
2267 # 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
2268 # 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
2269 # '\u4eba\u6c11'
83658
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2270 check_format('repr=\u4eba\u6c11',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2271 b'repr=%V', None, b'\xe4\xba\xba\xe6\xb0\x91')
68058
72f30f8ef331 Merged revisions 88697 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68087
diff changeset
2272
72f30f8ef331 Merged revisions 88697 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68087
diff changeset
2273 #Test replace error handler.
83658
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2274 check_format('repr=abc\ufffd',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2275 b'repr=%V', None, b'abc\xff')
68058
72f30f8ef331 Merged revisions 88697 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68087
diff changeset
2276
68203
87de017da4bf Issue #10831: PyUnicode_FromFormat() supports %li, %lli and %zi formats
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68201
diff changeset
2277 # not supported: copy the raw format string. these tests are just here
87de017da4bf Issue #10831: PyUnicode_FromFormat() supports %li, %lli and %zi formats
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68201
diff changeset
2278 # to check for crashs and should not be considered as specifications
83658
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2279 check_format('%s',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2280 b'%1%s', b'abc')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2281 check_format('%1abc',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2282 b'%1abc')
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2283 check_format('%+i',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2284 b'%+i', c_int(10))
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2285 check_format('%.%s',
9e0f1c3bf9b6 Issue #7330: Implement width and precision (ex: "%5.3s") for the format string
Victor Stinner <victor.stinner@gmail.com>
parents: 83203
diff changeset
2286 b'%.%s', b'abc')
68203
87de017da4bf Issue #10831: PyUnicode_FromFormat() supports %li, %lli and %zi formats
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68201
diff changeset
2287
65055
de7cdb726350 Issue #8870: PyUnicode_AsWideCharString() doesn't count the trailing nul character
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64724
diff changeset
2288 # 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
2289 def test_aswidechar(self):
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
2290 from _testcapi import unicode_aswidechar
66402
127f5168a774 Fix tests when ctypes isn't available
Antoine Pitrou <solipsis@pitrou.net>
parents: 66310
diff changeset
2291 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
2292 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
2293
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
2294 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
2295 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
2296 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
2297
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
2298 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
2299 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
2300 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
2301
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
2302 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
2303 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
2304 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
2305
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
2306 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
2307 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
2308 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
2309
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
2310 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
2311 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
2312 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
2313
65056
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
2314 nonbmp = chr(0x10ffff)
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
2315 if sizeof(c_wchar) == 2:
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
2316 buflen = 3
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
2317 nchar = 2
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
2318 else: # sizeof(c_wchar) == 4
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
2319 buflen = 2
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
2320 nchar = 1
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
2321 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
2322 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
2323 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
2324
65055
de7cdb726350 Issue #8870: PyUnicode_AsWideCharString() doesn't count the trailing nul character
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64724
diff changeset
2325 # 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
2326 def test_aswidecharstring(self):
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
2327 from _testcapi import unicode_aswidecharstring
66402
127f5168a774 Fix tests when ctypes isn't available
Antoine Pitrou <solipsis@pitrou.net>
parents: 66310
diff changeset
2328 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
2329 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
2330
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
2331 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
2332 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
2333 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
2334
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
2335 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
2336 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
2337 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
2338
65056
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
2339 nonbmp = chr(0x10ffff)
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
2340 if sizeof(c_wchar) == 2:
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
2341 nchar = 2
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
2342 else: # sizeof(c_wchar) == 4
0cc3ff8020ae Issue #8670: PyUnicode_AsWideChar() and PyUnicode_AsWideCharString() replace
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65055
diff changeset
2343 nchar = 1
65059
8de535def122 Issue #8670: Rename testcapi unicode test methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65058
diff changeset
2344 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
2345 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
2346 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
2347
72558
6da962d77eb3 remove "fast-path" for (i)adding strings
Benjamin Peterson <benjamin@python.org>
parents: 72480
diff changeset
2348 def test_subclass_add(self):
6da962d77eb3 remove "fast-path" for (i)adding strings
Benjamin Peterson <benjamin@python.org>
parents: 72480
diff changeset
2349 class S(str):
6da962d77eb3 remove "fast-path" for (i)adding strings
Benjamin Peterson <benjamin@python.org>
parents: 72480
diff changeset
2350 def __add__(self, o):
6da962d77eb3 remove "fast-path" for (i)adding strings
Benjamin Peterson <benjamin@python.org>
parents: 72480
diff changeset
2351 return "3"
6da962d77eb3 remove "fast-path" for (i)adding strings
Benjamin Peterson <benjamin@python.org>
parents: 72480
diff changeset
2352 self.assertEqual(S("4") + S("5"), "3")
6da962d77eb3 remove "fast-path" for (i)adding strings
Benjamin Peterson <benjamin@python.org>
parents: 72480
diff changeset
2353 class S(str):
6da962d77eb3 remove "fast-path" for (i)adding strings
Benjamin Peterson <benjamin@python.org>
parents: 72480
diff changeset
2354 def __iadd__(self, o):
6da962d77eb3 remove "fast-path" for (i)adding strings
Benjamin Peterson <benjamin@python.org>
parents: 72480
diff changeset
2355 return "3"
6da962d77eb3 remove "fast-path" for (i)adding strings
Benjamin Peterson <benjamin@python.org>
parents: 72480
diff changeset
2356 s = S("1")
6da962d77eb3 remove "fast-path" for (i)adding strings
Benjamin Peterson <benjamin@python.org>
parents: 72480
diff changeset
2357 s += "4"
6da962d77eb3 remove "fast-path" for (i)adding strings
Benjamin Peterson <benjamin@python.org>
parents: 72480
diff changeset
2358 self.assertEqual(s, "3")
6da962d77eb3 remove "fast-path" for (i)adding strings
Benjamin Peterson <benjamin@python.org>
parents: 72480
diff changeset
2359
73671
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2360 def test_encode_decimal(self):
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2361 from _testcapi import unicode_encodedecimal
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2362 self.assertEqual(unicode_encodedecimal('123'),
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2363 b'123')
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2364 self.assertEqual(unicode_encodedecimal('\u0663.\u0661\u0664'),
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2365 b'3.14')
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2366 self.assertEqual(unicode_encodedecimal("\N{EM SPACE}3.14\N{EN SPACE}"),
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2367 b' 3.14 ')
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2368 self.assertRaises(UnicodeEncodeError,
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2369 unicode_encodedecimal, "123\u20ac", "strict")
73741
a20fae95618c Close #13093: PyUnicode_EncodeDecimal() doesn't support error handlers
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73692
diff changeset
2370 self.assertRaisesRegex(
a20fae95618c Close #13093: PyUnicode_EncodeDecimal() doesn't support error handlers
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73692
diff changeset
2371 ValueError,
a20fae95618c Close #13093: PyUnicode_EncodeDecimal() doesn't support error handlers
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73692
diff changeset
2372 "^'decimal' codec can't encode character",
a20fae95618c Close #13093: PyUnicode_EncodeDecimal() doesn't support error handlers
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73692
diff changeset
2373 unicode_encodedecimal, "123\u20ac", "replace")
73671
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2374
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2375 def test_transform_decimal(self):
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2376 from _testcapi import unicode_transformdecimaltoascii as transform_decimal
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2377 self.assertEqual(transform_decimal('123'),
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2378 '123')
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2379 self.assertEqual(transform_decimal('\u0663.\u0661\u0664'),
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2380 '3.14')
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2381 self.assertEqual(transform_decimal("\N{EM SPACE}3.14\N{EN SPACE}"),
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2382 "\N{EM SPACE}3.14\N{EN SPACE}")
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2383 self.assertEqual(transform_decimal('123\u20ac'),
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2384 '123\u20ac')
849e9277906a Rewrite PyUnicode_EncodeDecimal() to use the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73579
diff changeset
2385
73676
5d5133f518cb Add a test on str.__getnewargs__()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73671
diff changeset
2386 def test_getnewargs(self):
5d5133f518cb Add a test on str.__getnewargs__()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73671
diff changeset
2387 text = 'abc'
5d5133f518cb Add a test on str.__getnewargs__()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73671
diff changeset
2388 args = text.__getnewargs__()
5d5133f518cb Add a test on str.__getnewargs__()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73671
diff changeset
2389 self.assertIsNot(args[0], text)
5d5133f518cb Add a test on str.__getnewargs__()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73671
diff changeset
2390 self.assertEqual(args[0], text)
5d5133f518cb Add a test on str.__getnewargs__()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73671
diff changeset
2391 self.assertEqual(len(args), 1)
5d5133f518cb Add a test on str.__getnewargs__()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73671
diff changeset
2392
82058
3b316ea5aa82 Issue #17137: When an Unicode string is resized, the internal wide character
Victor Stinner <victor.stinner@gmail.com>
parents: 81356
diff changeset
2393 def test_resize(self):
3b316ea5aa82 Issue #17137: When an Unicode string is resized, the internal wide character
Victor Stinner <victor.stinner@gmail.com>
parents: 81356
diff changeset
2394 for length in range(1, 100, 7):
3b316ea5aa82 Issue #17137: When an Unicode string is resized, the internal wide character
Victor Stinner <victor.stinner@gmail.com>
parents: 81356
diff changeset
2395 # generate a fresh string (refcount=1)
3b316ea5aa82 Issue #17137: When an Unicode string is resized, the internal wide character
Victor Stinner <victor.stinner@gmail.com>
parents: 81356
diff changeset
2396 text = 'a' * length + 'b'
3b316ea5aa82 Issue #17137: When an Unicode string is resized, the internal wide character
Victor Stinner <victor.stinner@gmail.com>
parents: 81356
diff changeset
2397
82285
7f34f8fa799d Silence DeprecationWarnings in test_unicode.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 82058
diff changeset
2398 with support.check_warnings(('unicode_internal codec has been '
7f34f8fa799d Silence DeprecationWarnings in test_unicode.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 82058
diff changeset
2399 'deprecated', DeprecationWarning)):
7f34f8fa799d Silence DeprecationWarnings in test_unicode.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 82058
diff changeset
2400 # fill wstr internal field
7f34f8fa799d Silence DeprecationWarnings in test_unicode.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 82058
diff changeset
2401 abc = text.encode('unicode_internal')
7f34f8fa799d Silence DeprecationWarnings in test_unicode.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 82058
diff changeset
2402 self.assertEqual(abc.decode('unicode_internal'), text)
82058
3b316ea5aa82 Issue #17137: When an Unicode string is resized, the internal wide character
Victor Stinner <victor.stinner@gmail.com>
parents: 81356
diff changeset
2403
82285
7f34f8fa799d Silence DeprecationWarnings in test_unicode.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 82058
diff changeset
2404 # resize text: wstr field must be cleared and then recomputed
7f34f8fa799d Silence DeprecationWarnings in test_unicode.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 82058
diff changeset
2405 text += 'c'
7f34f8fa799d Silence DeprecationWarnings in test_unicode.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 82058
diff changeset
2406 abcdef = text.encode('unicode_internal')
7f34f8fa799d Silence DeprecationWarnings in test_unicode.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 82058
diff changeset
2407 self.assertNotEqual(abc, abcdef)
7f34f8fa799d Silence DeprecationWarnings in test_unicode.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 82058
diff changeset
2408 self.assertEqual(abcdef.decode('unicode_internal'), text)
82058
3b316ea5aa82 Issue #17137: When an Unicode string is resized, the internal wide character
Victor Stinner <victor.stinner@gmail.com>
parents: 81356
diff changeset
2409
83203
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2410 def test_compare(self):
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2411 # Issue #17615
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2412 N = 10
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2413 ascii = 'a' * N
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2414 ascii2 = 'z' * N
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2415 latin = '\x80' * N
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2416 latin2 = '\xff' * N
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2417 bmp = '\u0100' * N
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2418 bmp2 = '\uffff' * N
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2419 astral = '\U00100000' * N
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2420 astral2 = '\U0010ffff' * N
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2421 strings = (
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2422 ascii, ascii2,
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2423 latin, latin2,
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2424 bmp, bmp2,
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2425 astral, astral2)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2426 for text1, text2 in itertools.combinations(strings, 2):
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2427 equal = (text1 is text2)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2428 self.assertEqual(text1 == text2, equal)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2429 self.assertEqual(text1 != text2, not equal)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2430
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2431 if equal:
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2432 self.assertTrue(text1 <= text2)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2433 self.assertTrue(text1 >= text2)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2434
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2435 # text1 is text2: duplicate strings to skip the "str1 == str2"
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2436 # optimization in unicode_compare_eq() and really compare
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2437 # character per character
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2438 copy1 = duplicate_string(text1)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2439 copy2 = duplicate_string(text2)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2440 self.assertIsNot(copy1, copy2)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2441
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2442 self.assertTrue(copy1 == copy2)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2443 self.assertFalse(copy1 != copy2)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2444
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2445 self.assertTrue(copy1 <= copy2)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2446 self.assertTrue(copy2 >= copy2)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2447
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2448 self.assertTrue(ascii < ascii2)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2449 self.assertTrue(ascii < latin)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2450 self.assertTrue(ascii < bmp)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2451 self.assertTrue(ascii < astral)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2452 self.assertFalse(ascii >= ascii2)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2453 self.assertFalse(ascii >= latin)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2454 self.assertFalse(ascii >= bmp)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2455 self.assertFalse(ascii >= astral)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2456
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2457 self.assertFalse(latin < ascii)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2458 self.assertTrue(latin < latin2)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2459 self.assertTrue(latin < bmp)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2460 self.assertTrue(latin < astral)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2461 self.assertTrue(latin >= ascii)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2462 self.assertFalse(latin >= latin2)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2463 self.assertFalse(latin >= bmp)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2464 self.assertFalse(latin >= astral)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2465
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2466 self.assertFalse(bmp < ascii)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2467 self.assertFalse(bmp < latin)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2468 self.assertTrue(bmp < bmp2)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2469 self.assertTrue(bmp < astral)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2470 self.assertTrue(bmp >= ascii)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2471 self.assertTrue(bmp >= latin)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2472 self.assertFalse(bmp >= bmp2)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2473 self.assertFalse(bmp >= astral)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2474
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2475 self.assertFalse(astral < ascii)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2476 self.assertFalse(astral < latin)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2477 self.assertFalse(astral < bmp2)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2478 self.assertTrue(astral < astral2)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2479 self.assertTrue(astral >= ascii)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2480 self.assertTrue(astral >= latin)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2481 self.assertTrue(astral >= bmp2)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2482 self.assertFalse(astral >= astral2)
db4a1a3d1f90 Issue #17615: Add tests comparing Unicode strings of different kinds
Victor Stinner <victor.stinner@gmail.com>
parents: 82287
diff changeset
2483
65055
de7cdb726350 Issue #8870: PyUnicode_AsWideCharString() doesn't count the trailing nul character
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64724
diff changeset
2484
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
2485 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
2486 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
2487 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
2488 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
2489
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
2490 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
2491 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
2492 ('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
2493 ('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
2494 ('', '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
2495 (' ', '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
2496 ])
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
2497
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
2498 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
2499 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
2500 ('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
2501 (' 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
2502 ])
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
2503
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
2504 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
2505 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
2506 ('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
2507 ])
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
2508
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
2509 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
2510 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
2511
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
2512 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
2513 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
2514 ('', '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
2515 ])
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
2516
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
2517 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
2518
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
2519 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
2520 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
2521 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
2522 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
2523 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
2524 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
2525 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
2526 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
2527 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
2528 "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
2529 [(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
2530 (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
2531 (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
2532 ]])
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
2533 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
2534
48601
a476ef5eebad Merged revisions 65339-65340,65342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48570
diff changeset
2535
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
2536 if __name__ == "__main__":
81356
55383912c225 #16910: test_bytes, test_unicode, and test_userstring now work with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80542
diff changeset
2537 unittest.main()