annotate Lib/test/test_unicode.py @ 65035:87f4b32905df 2.7

Cosmetic fix to use print function.
author Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
date Fri, 01 Oct 2010 10:48:47 +0000
parents 44199aa8932b
children ebb82f01dbe2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24538
8d5e0e3f6445 Add encoding declaration.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
1 # -*- coding: iso-8859-1 -*-
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 """ Test script for the Unicode implementation.
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 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
5
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 (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
7
15957
c1a8deb15f0e This patch removes all uses of "assert" in the regression test suite
Marc-André Lemburg <mal@egenix.com>
parents: 15910
diff changeset
8 """#"
45468
1d84bf5f9478 Patch #2167 from calvin: Remove unused imports
Christian Heimes <christian@cheimes.de>
parents: 45391
diff changeset
9 import sys, struct, codecs
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
10 from test import test_support, string_tests
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
11
35938
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
12 # 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
13 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
14 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
15 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
16 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
17 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
18 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
19 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
20 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
21 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
22 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
23 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
24 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
25 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
26 else:
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
27 return None
bb80b8f393ef Move registration of the codec search function to the module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 35331
diff changeset
28 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
29
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
30 class UnicodeTest(
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
31 string_tests.CommonTest,
33298
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
32 string_tests.MixinStrUnicodeUserStringTest,
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
33 string_tests.MixinStrUnicodeTest,
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
34 ):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
35 type2test = unicode
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
36
64708
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
37 def assertEqual(self, first, second, msg=None):
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
38 # strict assertEqual method: reject implicit bytes/unicode equality
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
39 super(UnicodeTest, self).assertEqual(first, second, msg)
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
40 if isinstance(first, unicode) or isinstance(second, unicode):
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
41 self.assertIsInstance(first, unicode)
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
42 self.assertIsInstance(second, unicode)
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
43 elif isinstance(first, str) or isinstance(second, str):
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
44 self.assertIsInstance(first, str)
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
45 self.assertIsInstance(second, str)
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
46
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
47 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
48 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
49 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
50 self.assertEqual(realresult, result)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54267
diff changeset
51 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
52
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
53 # 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
54 # 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
55 if realresult is object:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
56 class usub(unicode):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
57 def __repr__(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
58 return 'usub(%r)' % unicode.__repr__(self)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
59 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
60 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
61 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
62 self.assertEqual(realresult, result)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54267
diff changeset
63 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
64
30653
3b47d6057151 Fix for SF bug [ 817156 ] invalid \U escape gives 0=length unistr.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30059
diff changeset
65 def test_literals(self):
3b47d6057151 Fix for SF bug [ 817156 ] invalid \U escape gives 0=length unistr.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30059
diff changeset
66 self.assertEqual(u'\xff', u'\u00ff')
3b47d6057151 Fix for SF bug [ 817156 ] invalid \U escape gives 0=length unistr.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30059
diff changeset
67 self.assertEqual(u'\uffff', u'\U0000ffff')
42310
4c658377b908 Fix failing unicode test caused by change to ast.c at r56441
Kurt B. Kaiser <kbk@shore.net>
parents: 42023
diff changeset
68 self.assertRaises(SyntaxError, eval, 'u\'\\Ufffffffe\'')
4c658377b908 Fix failing unicode test caused by change to ast.c at r56441
Kurt B. Kaiser <kbk@shore.net>
parents: 42023
diff changeset
69 self.assertRaises(SyntaxError, eval, 'u\'\\Uffffffff\'')
4c658377b908 Fix failing unicode test caused by change to ast.c at r56441
Kurt B. Kaiser <kbk@shore.net>
parents: 42023
diff changeset
70 self.assertRaises(SyntaxError, eval, 'u\'\\U%08x\'' % 0x110000)
30653
3b47d6057151 Fix for SF bug [ 817156 ] invalid \U escape gives 0=length unistr.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30059
diff changeset
71
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
72 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
73 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
74 # Test basic sanity of repr()
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
75 self.assertEqual(repr(u'abc'), "u'abc'")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
76 self.assertEqual(repr(u'ab\\c'), "u'ab\\\\c'")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
77 self.assertEqual(repr(u'ab\\'), "u'ab\\\\'")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
78 self.assertEqual(repr(u'\\c'), "u'\\\\c'")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
79 self.assertEqual(repr(u'\\'), "u'\\\\'")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
80 self.assertEqual(repr(u'\n'), "u'\\n'")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
81 self.assertEqual(repr(u'\r'), "u'\\r'")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
82 self.assertEqual(repr(u'\t'), "u'\\t'")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
83 self.assertEqual(repr(u'\b'), "u'\\x08'")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
84 self.assertEqual(repr(u"'\""), """u'\\'"'""")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
85 self.assertEqual(repr(u"'\""), """u'\\'"'""")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
86 self.assertEqual(repr(u"'"), '''u"'"''')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
87 self.assertEqual(repr(u'"'), """u'"'""")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
88 latin1repr = (
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
89 "u'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r"
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
90 "\\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
91 "\\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
92 "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
93 "\\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
94 "\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b"
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
95 "\\x9c\\x9d\\x9e\\x9f\\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\\xa8\\xa9"
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
96 "\\xaa\\xab\\xac\\xad\\xae\\xaf\\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\xb6\\xb7"
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
97 "\\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf\\xc0\\xc1\\xc2\\xc3\\xc4\\xc5"
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
98 "\\xc6\\xc7\\xc8\\xc9\\xca\\xcb\\xcc\\xcd\\xce\\xcf\\xd0\\xd1\\xd2\\xd3"
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
99 "\\xd4\\xd5\\xd6\\xd7\\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf\\xe0\\xe1"
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
100 "\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\\xe8\\xe9\\xea\\xeb\\xec\\xed\\xee\\xef"
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
101 "\\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd"
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
102 "\\xfe\\xff'")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
103 testrepr = repr(u''.join(map(unichr, xrange(256))))
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
104 self.assertEqual(testrepr, latin1repr)
39621
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 38096
diff changeset
105 # Test repr works on wide unicode escapes without overflow.
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 38096
diff changeset
106 self.assertEqual(repr(u"\U00010000" * 39 + u"\uffff" * 4096),
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 38096
diff changeset
107 repr(u"\U00010000" * 39 + u"\uffff" * 4096))
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 38096
diff changeset
108
20017
08446a986a56 Test basic functioning of unicode repr(). (If this breaks Jython,
Guido van Rossum <guido@python.org>
parents: 19999
diff changeset
109
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
110 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
111 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
112 # check mixed argument types
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
113 self.checkequalnofix(3, 'aaa', 'count', u'a')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
114 self.checkequalnofix(0, 'aaa', 'count', u'b')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
115 self.checkequalnofix(3, u'aaa', 'count', 'a')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
116 self.checkequalnofix(0, u'aaa', 'count', 'b')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
117 self.checkequalnofix(0, u'aaa', 'count', 'b')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
118 self.checkequalnofix(1, u'aaa', 'count', 'a', -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
119 self.checkequalnofix(3, u'aaa', 'count', 'a', -10)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
120 self.checkequalnofix(2, u'aaa', 'count', 'a', 0, -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
121 self.checkequalnofix(0, u'aaa', 'count', 'a', 0, -10)
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
123 def test_find(self):
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
124 self.checkequalnofix(0, u'abcdefghiabc', 'find', u'abc')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
125 self.checkequalnofix(9, u'abcdefghiabc', 'find', u'abc', 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
126 self.checkequalnofix(-1, u'abcdefghiabc', 'find', u'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
127
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
128 self.assertRaises(TypeError, u'hello'.find)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
129 self.assertRaises(TypeError, u'hello'.find, 42)
15910
f7f3be3d00b9 Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents: 15706
diff changeset
130
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
131 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
132 string_tests.CommonTest.test_rfind(self)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
133 # check mixed argument types
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
134 self.checkequalnofix(9, 'abcdefghiabc', 'rfind', u'abc')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
135 self.checkequalnofix(12, 'abcdefghiabc', 'rfind', u'')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
136 self.checkequalnofix(12, u'abcdefghiabc', 'rfind', '')
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
138 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
139 string_tests.CommonTest.test_index(self)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
140 # check mixed argument types
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
141 for (t1, t2) in ((str, unicode), (unicode, str)):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
142 self.checkequalnofix(0, t1('abcdefghiabc'), 'index', t2(''))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
143 self.checkequalnofix(3, t1('abcdefghiabc'), 'index', t2('def'))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
144 self.checkequalnofix(0, t1('abcdefghiabc'), 'index', t2('abc'))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
145 self.checkequalnofix(9, t1('abcdefghiabc'), 'index', t2('abc'), 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
146 self.assertRaises(ValueError, t1('abcdefghiabc').index, t2('hib'))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
147 self.assertRaises(ValueError, t1('abcdefghiab').index, t2('abc'), 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
148 self.assertRaises(ValueError, t1('abcdefghi').index, t2('ghi'), 8)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
149 self.assertRaises(ValueError, t1('abcdefghi').index, t2('ghi'), -1)
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
151 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
152 string_tests.CommonTest.test_rindex(self)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
153 # check mixed argument types
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
154 for (t1, t2) in ((str, unicode), (unicode, str)):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
155 self.checkequalnofix(12, t1('abcdefghiabc'), 'rindex', t2(''))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
156 self.checkequalnofix(3, t1('abcdefghiabc'), 'rindex', t2('def'))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
157 self.checkequalnofix(9, t1('abcdefghiabc'), 'rindex', t2('abc'))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
158 self.checkequalnofix(0, t1('abcdefghiabc'), 'rindex', t2('abc'), 0, -1)
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
160 self.assertRaises(ValueError, t1('abcdefghiabc').rindex, t2('hib'))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
161 self.assertRaises(ValueError, t1('defghiabc').rindex, t2('def'), 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
162 self.assertRaises(ValueError, t1('defghiabc').rindex, t2('abc'), 0, -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
163 self.assertRaises(ValueError, t1('abcdefghi').rindex, t2('ghi'), 0, 8)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
164 self.assertRaises(ValueError, t1('abcdefghi').rindex, t2('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
165
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
166 def test_translate(self):
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
167 self.checkequalnofix(u'bbbc', u'abababc', 'translate', {ord('a'):None})
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
168 self.checkequalnofix(u'iiic', u'abababc', 'translate', {ord('a'):None, ord('b'):ord('i')})
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
169 self.checkequalnofix(u'iiix', u'abababc', 'translate', {ord('a'):None, ord('b'):ord('i'), ord('c'):u'x'})
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
170 self.checkequalnofix(u'<i><i><i>c', u'abababc', 'translate', {ord('a'):None, ord('b'):u'<i>'})
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
171 self.checkequalnofix(u'c', u'abababc', 'translate', {ord('a'):None, ord('b'):u''})
31455
37d501b9e2d5 Fix reallocation bug in unicode.translate(): The code was comparing
Walter Dörwald <walter@livinglogic.de>
parents: 30653
diff changeset
172 self.checkequalnofix(u'xyyx', u'xzx', 'translate', {ord('z'):u'yy'})
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
173
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
174 self.assertRaises(TypeError, u'hello'.translate)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
175 self.assertRaises(TypeError, u'abababc'.translate, {ord('a'):''})
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
177 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
178 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
179
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
180 # Mixed arguments
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
181 self.checkequalnofix([u'a', u'b', u'c', u'd'], u'a//b//c//d', 'split', '//')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
182 self.checkequalnofix([u'a', u'b', u'c', u'd'], 'a//b//c//d', 'split', u'//')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
183 self.checkequalnofix([u'endcase ', u''], u'endcase test', 'split', 'test')
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
184
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
185 def test_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
186 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
187
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
188 # mixed arguments
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
189 self.checkequalnofix(u'a b c d', u' ', 'join', ['a', 'b', u'c', u'd'])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
190 self.checkequalnofix(u'abcd', u'', 'join', (u'a', u'b', u'c', u'd'))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
191 self.checkequalnofix(u'w x y z', u' ', 'join', string_tests.Sequence('wxyz'))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
192 self.checkequalnofix(u'a b c d', ' ', 'join', [u'a', u'b', u'c', u'd'])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
193 self.checkequalnofix(u'a b c d', ' ', 'join', ['a', 'b', u'c', u'd'])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
194 self.checkequalnofix(u'abcd', '', 'join', (u'a', u'b', u'c', u'd'))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
195 self.checkequalnofix(u'w x y z', ' ', 'join', string_tests.Sequence(u'wxyz'))
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
197 def test_strip(self):
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
198 string_tests.CommonTest.test_strip(self)
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
199 self.assertRaises(UnicodeError, u"hello".strip, "\xff")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
200
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
201 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
202 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
203
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
204 # method call forwarded from str implementation because of unicode argument
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
205 self.checkequalnofix(u'one@two!three!', 'one!two!three!', 'replace', u'!', u'@', 1)
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
206 self.assertRaises(TypeError, 'replace'.replace, u"r", 42)
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
208 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
209 # Comparisons:
64708
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
210 self.assertTrue(u'abc' == 'abc')
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
211 self.assertTrue('abc' == u'abc')
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
212 self.assertTrue(u'abc' == u'abc')
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54267
diff changeset
213 self.assertTrue(u'abcd' > 'abc')
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54267
diff changeset
214 self.assertTrue('abcd' > u'abc')
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54267
diff changeset
215 self.assertTrue(u'abcd' > u'abc')
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54267
diff changeset
216 self.assertTrue(u'abc' < 'abcd')
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54267
diff changeset
217 self.assertTrue('abc' < u'abcd')
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54267
diff changeset
218 self.assertTrue(u'abc' < u'abcd')
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
219
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
220 if 0:
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
221 # 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
222 # 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
223
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
224 # No surrogates, no fixup required.
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54267
diff changeset
225 self.assertTrue(u'\u0061' < u'\u20ac')
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
226 # Non surrogate below surrogate value, no fixup required
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54267
diff changeset
227 self.assertTrue(u'\u0061' < u'\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
228
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
229 # 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
230 def test_lecmp(s, s2):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54267
diff changeset
231 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
232
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
233 def test_fixup(s):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
234 s2 = u'\ud800\udc01'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
235 test_lecmp(s, s2)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
236 s2 = u'\ud900\udc01'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
237 test_lecmp(s, s2)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
238 s2 = u'\uda00\udc01'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
239 test_lecmp(s, s2)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
240 s2 = u'\udb00\udc01'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
241 test_lecmp(s, s2)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
242 s2 = u'\ud800\udd01'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
243 test_lecmp(s, s2)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
244 s2 = u'\ud900\udd01'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
245 test_lecmp(s, s2)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
246 s2 = u'\uda00\udd01'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
247 test_lecmp(s, s2)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
248 s2 = u'\udb00\udd01'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
249 test_lecmp(s, s2)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
250 s2 = u'\ud800\ude01'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
251 test_lecmp(s, s2)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
252 s2 = u'\ud900\ude01'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
253 test_lecmp(s, s2)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
254 s2 = u'\uda00\ude01'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
255 test_lecmp(s, s2)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
256 s2 = u'\udb00\ude01'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
257 test_lecmp(s, s2)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
258 s2 = u'\ud800\udfff'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
259 test_lecmp(s, s2)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
260 s2 = u'\ud900\udfff'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
261 test_lecmp(s, s2)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
262 s2 = u'\uda00\udfff'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
263 test_lecmp(s, s2)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
264 s2 = u'\udb00\udfff'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
265 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
266
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
267 test_fixup(u'\ue000')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
268 test_fixup(u'\uff61')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
269
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
270 # Surrogates on both sides, no fixup required
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54267
diff changeset
271 self.assertTrue(u'\ud800\udc02' < u'\ud84d\udc56')
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_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
274 string_tests.MixinStrUnicodeUserStringTest.test_islower(self)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
275 self.checkequalnofix(False, u'\u1FFc', 'islower')
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
276
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
277 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
278 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
279 if not sys.platform.startswith('java'):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
280 self.checkequalnofix(False, u'\u1FFc', 'isupper')
13353
0b289e00d52a Added tests for the new .isalpha() and .isalnum() methods.
Marc-André Lemburg <mal@egenix.com>
parents: 13189
diff changeset
281
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
282 def test_istitle(self):
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
283 string_tests.MixinStrUnicodeUserStringTest.test_title(self)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
284 self.checkequalnofix(True, u'\u1FFc', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
285 self.checkequalnofix(True, u'Greek \u1FFcitlecases ...', 'istitle')
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
286
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
287 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
288 string_tests.MixinStrUnicodeUserStringTest.test_isspace(self)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
289 self.checkequalnofix(True, u'\u2000', 'isspace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
290 self.checkequalnofix(True, u'\u200a', 'isspace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
291 self.checkequalnofix(False, u'\u2014', 'isspace')
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
292
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
293 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
294 string_tests.MixinStrUnicodeUserStringTest.test_isalpha(self)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
295 self.checkequalnofix(True, u'\u1FFc', '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
296
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
297 def test_isdecimal(self):
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
298 self.checkequalnofix(False, u'', 'isdecimal')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
299 self.checkequalnofix(False, u'a', 'isdecimal')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
300 self.checkequalnofix(True, u'0', 'isdecimal')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
301 self.checkequalnofix(False, u'\u2460', 'isdecimal') # CIRCLED DIGIT ONE
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
302 self.checkequalnofix(False, u'\xbc', 'isdecimal') # VULGAR FRACTION ONE QUARTER
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
303 self.checkequalnofix(True, u'\u0660', 'isdecimal') # ARABIC-INDIC DIGIT ZERO
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
304 self.checkequalnofix(True, u'0123456789', 'isdecimal')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
305 self.checkequalnofix(False, u'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
306
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
307 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
308
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
309 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
310 string_tests.MixinStrUnicodeUserStringTest.test_isdigit(self)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
311 self.checkequalnofix(True, u'\u2460', 'isdigit')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
312 self.checkequalnofix(False, u'\xbc', 'isdigit')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
313 self.checkequalnofix(True, u'\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
314
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
315 def test_isnumeric(self):
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
316 self.checkequalnofix(False, u'', 'isnumeric')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
317 self.checkequalnofix(False, u'a', 'isnumeric')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
318 self.checkequalnofix(True, u'0', 'isnumeric')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
319 self.checkequalnofix(True, u'\u2460', 'isnumeric')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
320 self.checkequalnofix(True, u'\xbc', 'isnumeric')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
321 self.checkequalnofix(True, u'\u0660', 'isnumeric')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
322 self.checkequalnofix(True, u'0123456789', 'isnumeric')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
323 self.checkequalnofix(False, u'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
324
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
325 self.assertRaises(TypeError, u"abc".isnumeric, 42)
12917
2a2caaee6797 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12897
diff changeset
326
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
327 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
328 # Testing Unicode contains method
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
329 self.assertIn('a', u'abdb')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
330 self.assertIn('a', u'bdab')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
331 self.assertIn('a', u'bdaba')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
332 self.assertIn('a', u'bdba')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
333 self.assertIn('a', u'bdba')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
334 self.assertIn(u'a', u'bdba')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
335 self.assertNotIn(u'a', u'bdb')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
336 self.assertNotIn(u'a', 'bdb')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
337 self.assertIn(u'a', 'bdba')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
338 self.assertIn(u'a', ('a',1,None))
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
339 self.assertIn(u'a', (1,None,'a'))
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
340 self.assertIn(u'a', (1,None,u'a'))
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
341 self.assertIn('a', ('a',1,None))
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
342 self.assertIn('a', (1,None,'a'))
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
343 self.assertIn('a', (1,None,u'a'))
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
344 self.assertNotIn('a', ('x',1,u'y'))
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
345 self.assertNotIn('a', ('x',1,None))
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
346 self.assertNotIn(u'abcd', u'abcxxxx')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
347 self.assertIn(u'ab', u'abcd')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
348 self.assertIn('ab', u'abc')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
349 self.assertIn(u'ab', 'abc')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
350 self.assertIn(u'ab', (1,None,u'ab'))
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
351 self.assertIn(u'', u'abc')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
352 self.assertIn('', u'abc')
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
353
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
354 # If the following fails either
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
355 # the contains operator does not propagate UnicodeErrors or
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
356 # someone has changed the default encoding
57729
2b8f485b0d03 Issue #1680159: unicode coercion during an 'in' operation was masking
R. David Murray <rdmurray@bitdance.com>
parents: 56093
diff changeset
357 self.assertRaises(UnicodeDecodeError, 'g\xe2teau'.__contains__, u'\xe2')
2b8f485b0d03 Issue #1680159: unicode coercion during an 'in' operation was masking
R. David Murray <rdmurray@bitdance.com>
parents: 56093
diff changeset
358 self.assertRaises(UnicodeDecodeError, u'g\xe2teau'.__contains__, '\xe2')
24737
eaa87632b1e8 Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents: 24703
diff changeset
359
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
360 self.assertIn(u'', '')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
361 self.assertIn('', u'')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
362 self.assertIn(u'', u'')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
363 self.assertIn(u'', 'abc')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
364 self.assertIn('', u'abc')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
365 self.assertIn(u'', u'abc')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
366 self.assertNotIn(u'\0', 'abc')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
367 self.assertNotIn('\0', u'abc')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
368 self.assertNotIn(u'\0', u'abc')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
369 self.assertIn(u'\0', '\0abc')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
370 self.assertIn('\0', u'\0abc')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
371 self.assertIn(u'\0', u'\0abc')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
372 self.assertIn(u'\0', 'abc\0')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
373 self.assertIn('\0', u'abc\0')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
374 self.assertIn(u'\0', u'abc\0')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
375 self.assertIn(u'a', '\0abc')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
376 self.assertIn('a', u'\0abc')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
377 self.assertIn(u'a', u'\0abc')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
378 self.assertIn(u'asdf', 'asdf')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
379 self.assertIn('asdf', u'asdf')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
380 self.assertIn(u'asdf', u'asdf')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
381 self.assertNotIn(u'asdf', 'asd')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
382 self.assertNotIn('asdf', u'asd')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
383 self.assertNotIn(u'asdf', u'asd')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
384 self.assertNotIn(u'asdf', '')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
385 self.assertNotIn('asdf', u'')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58090
diff changeset
386 self.assertNotIn(u'asdf', u'')
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
387
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
388 self.assertRaises(TypeError, u"abc".__contains__)
57729
2b8f485b0d03 Issue #1680159: unicode coercion during an 'in' operation was masking
R. David Murray <rdmurray@bitdance.com>
parents: 56093
diff changeset
389 self.assertRaises(TypeError, u"abc".__contains__, object())
11992
1789fae5f074 Marc-Andre Lemburg: test script for Unicode implementation.
Guido van Rossum <guido@python.org>
parents:
diff changeset
390
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
391 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
392 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
393 # Testing Unicode formatting strings...
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
394 self.assertEqual(u"%s, %s" % (u"abc", "abc"), u'abc, abc')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
395 self.assertEqual(u"%s, %s, %i, %f, %5.2f" % (u"abc", "abc", 1, 2, 3), u'abc, abc, 1, 2.000000, 3.00')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
396 self.assertEqual(u"%s, %s, %i, %f, %5.2f" % (u"abc", "abc", 1, -2, 3), u'abc, abc, 1, -2.000000, 3.00')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
397 self.assertEqual(u"%s, %s, %i, %f, %5.2f" % (u"abc", "abc", -1, -2, 3.5), u'abc, abc, -1, -2.000000, 3.50')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
398 self.assertEqual(u"%s, %s, %i, %f, %5.2f" % (u"abc", "abc", -1, -2, 3.57), u'abc, abc, -1, -2.000000, 3.57')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
399 self.assertEqual(u"%s, %s, %i, %f, %5.2f" % (u"abc", "abc", -1, -2, 1003.57), u'abc, abc, -1, -2.000000, 1003.57')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
400 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
401 self.assertEqual(u"%r, %r" % (u"abc", "abc"), u"u'abc', 'abc'")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
402 self.assertEqual(u"%(x)s, %(y)s" % {'x':u"abc", 'y':"def"}, u'abc, def')
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
403 self.assertEqual(u"%(x)s, %(\xfc)s" % {'x':u"abc", u'\xfc':"def"}, u'abc, def')
28479
e13a7e1fdb9e Remove duplicate test.
Walter Dörwald <walter@livinglogic.de>
parents: 28478
diff changeset
404
28478
8802de0da533 Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents: 28009
diff changeset
405 self.assertEqual(u'%c' % 0x1234, u'\u1234')
28494
32bea41fef68 Change formatchar(), so that u"%c" % 0xffffffff now raises
Walter Dörwald <walter@livinglogic.de>
parents: 28479
diff changeset
406 self.assertRaises(OverflowError, u"%c".__mod__, (sys.maxunicode+1,))
63102
3fa605688d03 Sub-issue of #9036: Fix incorrect use of Py_CHARMASK.
Stefan Krah <stefan@bytereef.org>
parents: 62173
diff changeset
407 self.assertRaises(ValueError, u"%.1\u1032f".__mod__, (1.0/3))
20563
9058e49c6c57 Additional test and documentation for the unicode() changes.
Marc-André Lemburg <mal@egenix.com>
parents: 20240
diff changeset
408
59085
32b21061ee9f Issue #7649: Fix u'%c' % char for character in range 0x80..0xFF
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58460
diff changeset
409 for num in range(0x00,0x80):
32b21061ee9f Issue #7649: Fix u'%c' % char for character in range 0x80..0xFF
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58460
diff changeset
410 char = chr(num)
64708
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
411 self.assertEqual(u"%c" % char, unicode(char))
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
412 self.assertEqual(u"%c" % num, unicode(char))
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
413 self.assertTrue(char == u"%c" % char)
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
414 self.assertTrue(char == u"%c" % num)
59085
32b21061ee9f Issue #7649: Fix u'%c' % char for character in range 0x80..0xFF
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58460
diff changeset
415 # Issue 7649
32b21061ee9f Issue #7649: Fix u'%c' % char for character in range 0x80..0xFF
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58460
diff changeset
416 for num in range(0x80,0x100):
32b21061ee9f Issue #7649: Fix u'%c' % char for character in range 0x80..0xFF
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58460
diff changeset
417 uchar = unichr(num)
32b21061ee9f Issue #7649: Fix u'%c' % char for character in range 0x80..0xFF
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58460
diff changeset
418 self.assertEqual(uchar, u"%c" % num) # works only with ints
32b21061ee9f Issue #7649: Fix u'%c' % char for character in range 0x80..0xFF
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58460
diff changeset
419 self.assertEqual(uchar, u"%c" % uchar) # and unicode chars
32b21061ee9f Issue #7649: Fix u'%c' % char for character in range 0x80..0xFF
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58460
diff changeset
420 # the implicit decoding should fail for non-ascii chars
32b21061ee9f Issue #7649: Fix u'%c' % char for character in range 0x80..0xFF
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58460
diff changeset
421 self.assertRaises(UnicodeDecodeError, u"%c".__mod__, chr(num))
32b21061ee9f Issue #7649: Fix u'%c' % char for character in range 0x80..0xFF
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58460
diff changeset
422 self.assertRaises(UnicodeDecodeError, u"%s".__mod__, chr(num))
32b21061ee9f Issue #7649: Fix u'%c' % char for character in range 0x80..0xFF
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58460
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 # formatting jobs delegated from the string implementation:
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
425 self.assertEqual('...%(foo)s...' % {'foo':u"abc"}, u'...abc...')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
426 self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
427 self.assertEqual('...%(foo)s...' % {u'foo':"abc"}, '...abc...')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
428 self.assertEqual('...%(foo)s...' % {u'foo':u"abc"}, u'...abc...')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
429 self.assertEqual('...%(foo)s...' % {u'foo':u"abc",'def':123}, u'...abc...')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
430 self.assertEqual('...%(foo)s...' % {u'foo':u"abc",u'def':123}, u'...abc...')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
431 self.assertEqual('...%s...%s...%s...%s...' % (1,2,3,u"abc"), u'...1...2...3...abc...')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
432 self.assertEqual('...%%...%%s...%s...%s...%s...%s...' % (1,2,3,u"abc"), u'...%...%s...1...2...3...abc...')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
433 self.assertEqual('...%s...' % u"abc", u'...abc...')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
434 self.assertEqual('%*s' % (5,u'abc',), u' abc')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
435 self.assertEqual('%*s' % (-5,u'abc',), u'abc ')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
436 self.assertEqual('%*.*s' % (5,2,u'abc',), u' ab')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
437 self.assertEqual('%*.*s' % (5,3,u'abc',), u' abc')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
438 self.assertEqual('%i %*.*s' % (10, 5,3,u'abc',), u'10 abc')
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27797
diff changeset
439 self.assertEqual('%i%s %*.*s' % (10, 3, 5, 3, u'abc',), u'103 abc')
28478
8802de0da533 Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents: 28009
diff changeset
440 self.assertEqual('%c' % u'a', u'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
441 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
442 def __str__(self):
ef11cbc08dab Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 34931
diff changeset
443 return u'\u1234'
ef11cbc08dab Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 34931
diff changeset
444 self.assertEqual('%s' % Wrapper(), u'\u1234')
38096
5457ab9f8599 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38055
diff changeset
445
38055
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 37301
diff changeset
446 @test_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
447 def test_format_float(self):
38055
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 37301
diff changeset
448 # should not format with a comma, but always with C locale
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 37301
diff changeset
449 self.assertEqual(u'1.0', u'%.1f' % 1.0)
36341
4bcf5fe54d83 Checkin the test of patch #1400181.
Georg Brandl <georg@python.org>
parents: 36056
diff changeset
450
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
451 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
452 # 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
453
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
454 self.assertEqual(
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
455 unicode(u'unicode remains unicode'),
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
456 u'unicode remains unicode'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
457 )
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
458
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
459 class UnicodeSubclass(unicode):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
460 pass
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 18828
diff changeset
461
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
462 self.assertEqual(
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
463 unicode(UnicodeSubclass('unicode subclass becomes unicode')),
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
464 u'unicode subclass becomes unicode'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
465 )
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
466
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
467 self.assertEqual(
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
468 unicode('strings are converted to unicode'),
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
469 u'strings are converted to unicode'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
470 )
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 18828
diff changeset
471
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
472 class UnicodeCompat:
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
473 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
474 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
475 def __unicode__(self):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
476 return self.x
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 18828
diff changeset
477
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
478 self.assertEqual(
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
479 unicode(UnicodeCompat('__unicode__ compatible objects are recognized')),
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
480 u'__unicode__ compatible objects are recognized')
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 18828
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 class StringCompat:
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
483 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
484 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
485 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
486 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
487
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
488 self.assertEqual(
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
489 unicode(StringCompat('__str__ compatible objects are recognized')),
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
490 u'__str__ compatible objects are recognized'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
491 )
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
492
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
493 # 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
494
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
495 o = StringCompat('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
496 self.assertEqual(unicode(o), u'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
497 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
498
32801
16de4f90ef44 Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
Marc-André Lemburg <mal@egenix.com>
parents: 32240
diff changeset
499 # %-formatting and .__unicode__()
16de4f90ef44 Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
Marc-André Lemburg <mal@egenix.com>
parents: 32240
diff changeset
500 self.assertEqual(u'%s' %
16de4f90ef44 Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
Marc-André Lemburg <mal@egenix.com>
parents: 32240
diff changeset
501 UnicodeCompat(u"u'%s' % obj uses obj.__unicode__()"),
16de4f90ef44 Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
Marc-André Lemburg <mal@egenix.com>
parents: 32240
diff changeset
502 u"u'%s' % obj uses obj.__unicode__()")
16de4f90ef44 Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
Marc-André Lemburg <mal@egenix.com>
parents: 32240
diff changeset
503 self.assertEqual(u'%s' %
16de4f90ef44 Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
Marc-André Lemburg <mal@egenix.com>
parents: 32240
diff changeset
504 UnicodeCompat(u"u'%s' % obj falls back to obj.__str__()"),
16de4f90ef44 Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
Marc-André Lemburg <mal@egenix.com>
parents: 32240
diff changeset
505 u"u'%s' % obj falls back to obj.__str__()")
16de4f90ef44 Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
Marc-André Lemburg <mal@egenix.com>
parents: 32240
diff changeset
506
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
507 for obj in (123, 123.45, 123L):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
508 self.assertEqual(unicode(obj), unicode(str(obj)))
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
509
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
510 # 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
511 # 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
512
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
513 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
514 self.assertRaises(
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
515 TypeError,
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
516 unicode,
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
517 u'decoding unicode is not supported',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
518 'utf-8',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
519 'strict'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
520 )
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
521
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
522 self.assertEqual(
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
523 unicode('strings are decoded to unicode', 'utf-8', 'strict'),
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
524 u'strings are decoded to unicode'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
525 )
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
526
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
527 if not sys.platform.startswith('java'):
59432
e23cba03cc4b Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59085
diff changeset
528 with test_support.check_py3k_warnings():
58090
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57729
diff changeset
529 buf = buffer('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
530 self.assertEqual(
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
531 unicode(
58090
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57729
diff changeset
532 buf,
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
533 'utf-8',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
534 'strict'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
535 ),
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
536 u'character buffers are decoded to unicode'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
537 )
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
538
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
539 self.assertRaises(TypeError, unicode, 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
540
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
541 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
542 utfTests = [
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
543 (u'A\u2262\u0391.', 'A+ImIDkQ.'), # RFC2152 example
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
544 (u'Hi Mom -\u263a-!', 'Hi Mom -+Jjo--!'), # RFC2152 example
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
545 (u'\u65E5\u672C\u8A9E', '+ZeVnLIqe-'), # RFC2152 example
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
546 (u'Item 3 is \u00a31.', 'Item 3 is +AKM-1.'), # RFC2152 example
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
547 (u'+', '+-'),
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
548 (u'+-', '+--'),
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
549 (u'+?', '+-?'),
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
550 (u'\?', '+AFw?'),
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
551 (u'+?', '+-?'),
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
552 (ur'\\?', '+AFwAXA?'),
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
553 (ur'\\\?', '+AFwAXABc?'),
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 52161
diff changeset
554 (ur'++--', '+-+---'),
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 52161
diff changeset
555 (u'\U000abcde', '+2m/c3g-'), # surrogate pairs
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 52161
diff changeset
556 (u'/', '/'),
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
557 ]
12051
d89556c04ef9 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
558
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
559 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
560 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
561
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 52161
diff changeset
562 # Unpaired surrogates 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
563 self.assertRaises(UnicodeError, unicode, '+3ADYAA-', 'utf-7')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
564
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 52161
diff changeset
565 self.assertEqual(unicode('+3ADYAA-', 'utf-7', 'replace'), u'\ufffd\ufffd')
12331
d195f17f2927 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
566
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 52161
diff changeset
567 # Direct encoded characters
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 52161
diff changeset
568 set_d = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'(),-./:?"
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 52161
diff changeset
569 # Optional direct characters
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 52161
diff changeset
570 set_o = '!"#$%&*;<=>@[]^_`{|}'
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 52161
diff changeset
571 for c in set_d:
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 52161
diff changeset
572 self.assertEqual(c.encode('utf7'), c.encode('ascii'))
64708
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
573 self.assertEqual(c.encode('ascii').decode('utf7'), unicode(c))
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
574 self.assertTrue(c == c.encode('ascii').decode('utf7'))
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 52161
diff changeset
575 for c in set_o:
64708
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
576 self.assertEqual(c.encode('ascii').decode('utf7'), unicode(c))
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
577 self.assertTrue(c == c.encode('ascii').decode('utf7'))
48070
35a462ff9873 #2242: utf7 decoding crashes on bogus input on some Windows/MSVC versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 46028
diff changeset
578
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
579 def test_codecs_utf8(self):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
580 self.assertEqual(u''.encode('utf-8'), '')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
581 self.assertEqual(u'\u20ac'.encode('utf-8'), '\xe2\x82\xac')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
582 self.assertEqual(u'\ud800\udc02'.encode('utf-8'), '\xf0\x90\x80\x82')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
583 self.assertEqual(u'\ud84d\udc56'.encode('utf-8'), '\xf0\xa3\x91\x96')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
584 self.assertEqual(u'\ud800'.encode('utf-8'), '\xed\xa0\x80')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
585 self.assertEqual(u'\udc00'.encode('utf-8'), '\xed\xb0\x80')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
586 self.assertEqual(
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
587 (u'\ud800\udc02'*1000).encode('utf-8'),
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
588 '\xf0\x90\x80\x82'*1000
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
589 )
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
590 self.assertEqual(
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
591 u'\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
592 u'\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
593 u'\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
594 u'\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
595 u'\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
596 u' Nunstuck git und'.encode('utf-8'),
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
597 '\xe6\xad\xa3\xe7\xa2\xba\xe3\x81\xab\xe8\xa8\x80\xe3\x81'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
598 '\x86\xe3\x81\xa8\xe7\xbf\xbb\xe8\xa8\xb3\xe3\x81\xaf\xe3'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
599 '\x81\x95\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x84\xe3\x81\xbe'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
600 '\xe3\x81\x9b\xe3\x82\x93\xe3\x80\x82\xe4\xb8\x80\xe9\x83'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
601 '\xa8\xe3\x81\xaf\xe3\x83\x89\xe3\x82\xa4\xe3\x83\x84\xe8'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
602 '\xaa\x9e\xe3\x81\xa7\xe3\x81\x99\xe3\x81\x8c\xe3\x80\x81'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
603 '\xe3\x81\x82\xe3\x81\xa8\xe3\x81\xaf\xe3\x81\xa7\xe3\x81'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
604 '\x9f\xe3\x82\x89\xe3\x82\x81\xe3\x81\xa7\xe3\x81\x99\xe3'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
605 '\x80\x82\xe5\xae\x9f\xe9\x9a\x9b\xe3\x81\xab\xe3\x81\xaf'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
606 '\xe3\x80\x8cWenn ist das Nunstuck git und'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
607 )
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
608
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
609 # UTF-8 specific decoding tests
64701
28e6a41c5ba0 Merged revisions 84470-84471,84566-84567,84759 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63102
diff changeset
610 self.assertEqual(unicode('\xf0\xa3\x91\x96', 'utf-8'), u'\U00023456')
28e6a41c5ba0 Merged revisions 84470-84471,84566-84567,84759 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63102
diff changeset
611 self.assertEqual(unicode('\xf0\x90\x80\x82', 'utf-8'), u'\U00010002')
28e6a41c5ba0 Merged revisions 84470-84471,84566-84567,84759 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63102
diff changeset
612 self.assertEqual(unicode('\xe2\x82\xac', 'utf-8'), u'\u20ac')
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
613
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
614 # 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
615 # * 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
616 # UTF8_ERROR cases in PyUnicode_DecodeUTF8
12051
d89556c04ef9 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
617
62119
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
618 def test_utf8_decode_valid_sequences(self):
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
619 sequences = [
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
620 # single byte
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
621 ('\x00', u'\x00'), ('a', u'a'), ('\x7f', u'\x7f'),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
622 # 2 bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
623 ('\xc2\x80', u'\x80'), ('\xdf\xbf', u'\u07ff'),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
624 # 3 bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
625 ('\xe0\xa0\x80', u'\u0800'), ('\xed\x9f\xbf', u'\ud7ff'),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
626 ('\xee\x80\x80', u'\uE000'), ('\xef\xbf\xbf', u'\uffff'),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
627 # 4 bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
628 ('\xF0\x90\x80\x80', u'\U00010000'),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
629 ('\xf4\x8f\xbf\xbf', u'\U0010FFFF')
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
630 ]
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
631 for seq, res in sequences:
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
632 self.assertEqual(seq.decode('utf-8'), res)
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
633
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
634 for ch in map(unichr, range(0, sys.maxunicode)):
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
635 self.assertEqual(ch, ch.encode('utf-8').decode('utf-8'))
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
636
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
637 def test_utf8_decode_invalid_sequences(self):
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
638 # continuation bytes in a sequence of 2, 3, or 4 bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
639 continuation_bytes = map(chr, range(0x80, 0xC0))
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
640 # start bytes of a 2-byte sequence equivalent to codepoints < 0x7F
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
641 invalid_2B_seq_start_bytes = map(chr, range(0xC0, 0xC2))
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
642 # start bytes of a 4-byte sequence equivalent to codepoints > 0x10FFFF
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
643 invalid_4B_seq_start_bytes = map(chr, range(0xF5, 0xF8))
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
644 invalid_start_bytes = (
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
645 continuation_bytes + invalid_2B_seq_start_bytes +
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
646 invalid_4B_seq_start_bytes + map(chr, range(0xF7, 0x100))
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
647 )
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
648
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
649 for byte in invalid_start_bytes:
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
650 self.assertRaises(UnicodeDecodeError, byte.decode, 'utf-8')
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
651
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
652 for sb in invalid_2B_seq_start_bytes:
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
653 for cb in continuation_bytes:
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
654 self.assertRaises(UnicodeDecodeError, (sb+cb).decode, 'utf-8')
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
655
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
656 for sb in invalid_4B_seq_start_bytes:
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
657 for cb1 in continuation_bytes[:3]:
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
658 for cb3 in continuation_bytes[:3]:
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
659 self.assertRaises(UnicodeDecodeError,
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
660 (sb+cb1+'\x80'+cb3).decode, 'utf-8')
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
661
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
662 for cb in map(chr, range(0x80, 0xA0)):
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
663 self.assertRaises(UnicodeDecodeError,
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
664 ('\xE0'+cb+'\x80').decode, 'utf-8')
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
665 self.assertRaises(UnicodeDecodeError,
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
666 ('\xE0'+cb+'\xBF').decode, 'utf-8')
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
667 # XXX: surrogates shouldn't be valid UTF-8!
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
668 # see http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
669 # (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
670 #for cb in map(chr, range(0xA0, 0xC0)):
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
671 #sys.__stdout__.write('\\xED\\x%02x\\x80\n' % ord(cb))
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
672 #self.assertRaises(UnicodeDecodeError,
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
673 #('\xED'+cb+'\x80').decode, 'utf-8')
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
674 #self.assertRaises(UnicodeDecodeError,
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
675 #('\xED'+cb+'\xBF').decode, 'utf-8')
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
676 for cb in map(chr, range(0x80, 0x90)):
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
677 self.assertRaises(UnicodeDecodeError,
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
678 ('\xF0'+cb+'\x80\x80').decode, 'utf-8')
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
679 self.assertRaises(UnicodeDecodeError,
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
680 ('\xF0'+cb+'\xBF\xBF').decode, 'utf-8')
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
681 for cb in map(chr, range(0x90, 0xC0)):
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
682 self.assertRaises(UnicodeDecodeError,
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
683 ('\xF4'+cb+'\x80\x80').decode, 'utf-8')
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
684 self.assertRaises(UnicodeDecodeError,
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
685 ('\xF4'+cb+'\xBF\xBF').decode, 'utf-8')
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
686
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
687 def test_issue8271(self):
62120
c1caee32544f Add a NEWS entry for r81758 and clarify a comment.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62119
diff changeset
688 # Issue #8271: during the decoding of an invalid UTF-8 byte sequence,
c1caee32544f Add a NEWS entry for r81758 and clarify a comment.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62119
diff changeset
689 # only the start byte and the continuation byte(s) are now considered
c1caee32544f Add a NEWS entry for r81758 and clarify a comment.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62119
diff changeset
690 # invalid, instead of the number of bytes specified by the start byte.
62119
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
691 # See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf (page 95,
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
692 # table 3-8, Row 2) for more information about the algorithm used.
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
693 FFFD = u'\ufffd'
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
694 sequences = [
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
695 # invalid start bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
696 ('\x80', FFFD), # continuation byte
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
697 ('\x80\x80', FFFD*2), # 2 continuation bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
698 ('\xc0', FFFD),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
699 ('\xc0\xc0', FFFD*2),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
700 ('\xc1', FFFD),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
701 ('\xc1\xc0', FFFD*2),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
702 ('\xc0\xc1', FFFD*2),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
703 # with start byte of a 2-byte sequence
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
704 ('\xc2', FFFD), # only the start byte
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
705 ('\xc2\xc2', FFFD*2), # 2 start bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
706 ('\xc2\xc2\xc2', FFFD*3), # 2 start bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
707 ('\xc2\x41', FFFD+'A'), # invalid continuation byte
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
708 # with start byte of a 3-byte sequence
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
709 ('\xe1', FFFD), # only the start byte
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
710 ('\xe1\xe1', FFFD*2), # 2 start bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
711 ('\xe1\xe1\xe1', FFFD*3), # 3 start bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
712 ('\xe1\xe1\xe1\xe1', FFFD*4), # 4 start bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
713 ('\xe1\x80', FFFD), # only 1 continuation byte
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
714 ('\xe1\x41', FFFD+'A'), # invalid continuation byte
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
715 ('\xe1\x41\x80', FFFD+'A'+FFFD), # invalid cb followed by valid cb
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
716 ('\xe1\x41\x41', FFFD+'AA'), # 2 invalid continuation bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
717 ('\xe1\x80\x41', FFFD+'A'), # only 1 valid continuation byte
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
718 ('\xe1\x80\xe1\x41', FFFD*2+'A'), # 1 valid and the other invalid
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
719 ('\xe1\x41\xe1\x80', FFFD+'A'+FFFD), # 1 invalid and the other valid
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
720 # with start byte of a 4-byte sequence
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
721 ('\xf1', FFFD), # only the start byte
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
722 ('\xf1\xf1', FFFD*2), # 2 start bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
723 ('\xf1\xf1\xf1', FFFD*3), # 3 start bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
724 ('\xf1\xf1\xf1\xf1', FFFD*4), # 4 start bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
725 ('\xf1\xf1\xf1\xf1\xf1', FFFD*5), # 5 start bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
726 ('\xf1\x80', FFFD), # only 1 continuation bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
727 ('\xf1\x80\x80', FFFD), # only 2 continuation bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
728 ('\xf1\x80\x41', FFFD+'A'), # 1 valid cb and 1 invalid
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
729 ('\xf1\x80\x41\x41', FFFD+'AA'), # 1 valid cb and 1 invalid
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
730 ('\xf1\x80\x80\x41', FFFD+'A'), # 2 valid cb and 1 invalid
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
731 ('\xf1\x41\x80', FFFD+'A'+FFFD), # 1 invalid cv and 1 valid
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
732 ('\xf1\x41\x80\x80', FFFD+'A'+FFFD*2), # 1 invalid cb and 2 invalid
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
733 ('\xf1\x41\x80\x41', FFFD+'A'+FFFD+'A'), # 2 invalid cb and 1 invalid
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
734 ('\xf1\x41\x41\x80', FFFD+'AA'+FFFD), # 1 valid cb and 1 invalid
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
735 ('\xf1\x41\xf1\x80', FFFD+'A'+FFFD),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
736 ('\xf1\x41\x80\xf1', FFFD+'A'+FFFD*2),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
737 ('\xf1\xf1\x80\x41', FFFD*2+'A'),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
738 ('\xf1\x41\xf1\xf1', FFFD+'A'+FFFD*2),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
739 # with invalid start byte of a 4-byte sequence (rfc2279)
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
740 ('\xf5', FFFD), # only the start byte
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
741 ('\xf5\xf5', FFFD*2), # 2 start bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
742 ('\xf5\x80', FFFD*2), # only 1 continuation byte
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
743 ('\xf5\x80\x80', FFFD*3), # only 2 continuation byte
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
744 ('\xf5\x80\x80\x80', FFFD*4), # 3 continuation bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
745 ('\xf5\x80\x41', FFFD*2+'A'), # 1 valid cb and 1 invalid
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
746 ('\xf5\x80\x41\xf5', FFFD*2+'A'+FFFD),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
747 ('\xf5\x41\x80\x80\x41', FFFD+'A'+FFFD*2+'A'),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
748 # with invalid start byte of a 5-byte sequence (rfc2279)
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
749 ('\xf8', FFFD), # only the start byte
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
750 ('\xf8\xf8', FFFD*2), # 2 start bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
751 ('\xf8\x80', FFFD*2), # only one continuation byte
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
752 ('\xf8\x80\x41', FFFD*2 + 'A'), # 1 valid cb and 1 invalid
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
753 ('\xf8\x80\x80\x80\x80', FFFD*5), # invalid 5 bytes seq with 5 bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
754 # with invalid start byte of a 6-byte sequence (rfc2279)
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
755 ('\xfc', FFFD), # only the start byte
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
756 ('\xfc\xfc', FFFD*2), # 2 start bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
757 ('\xfc\x80\x80', FFFD*3), # only 2 continuation bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
758 ('\xfc\x80\x80\x80\x80\x80', FFFD*6), # 6 continuation bytes
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
759 # invalid start byte
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
760 ('\xfe', FFFD),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
761 ('\xfe\x80\x80', FFFD*3),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
762 # other sequences
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
763 ('\xf1\x80\x41\x42\x43', u'\ufffd\x41\x42\x43'),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
764 ('\xf1\x80\xff\x42\x43', u'\ufffd\ufffd\x42\x43'),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
765 ('\xf1\x80\xc2\x81\x43', u'\ufffd\x81\x43'),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
766 ('\x61\xF1\x80\x80\xE1\x80\xC2\x62\x80\x63\x80\xBF\x64',
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
767 u'\x61\uFFFD\uFFFD\uFFFD\x62\uFFFD\x63\uFFFD\uFFFD\x64'),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
768 ]
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
769 for n, (seq, res) in enumerate(sequences):
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
770 self.assertRaises(UnicodeDecodeError, seq.decode, 'utf-8', 'strict')
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
771 self.assertEqual(seq.decode('utf-8', 'replace'), res)
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
772 self.assertEqual((seq+'b').decode('utf-8', 'replace'), res+'b')
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
773 self.assertEqual(seq.decode('utf-8', 'ignore'),
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
774 res.replace(u'\uFFFD', ''))
5032f1d5a1e1 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61884
diff changeset
775
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
776 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
777 # Test whether trailing dot is preserved
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
778 self.assertEqual(u"www.python.org.".encode("idna"), "www.python.org.")
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
779
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
780 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
781 # Error handling (encoding)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
782 self.assertRaises(UnicodeError, u'Andr\202 x'.encode, 'ascii')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
783 self.assertRaises(UnicodeError, u'Andr\202 x'.encode, 'ascii','strict')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
784 self.assertEqual(u'Andr\202 x'.encode('ascii','ignore'), "Andr x")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
785 self.assertEqual(u'Andr\202 x'.encode('ascii','replace'), "Andr? x")
56093
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
786 self.assertEqual(u'Andr\202 x'.encode('ascii', 'replace'),
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
787 u'Andr\202 x'.encode('ascii', errors='replace'))
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
788 self.assertEqual(u'Andr\202 x'.encode('ascii', 'ignore'),
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
789 u'Andr\202 x'.encode(encoding='ascii', errors='ignore'))
12051
d89556c04ef9 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
790
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
791 # Error handling (decoding)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
792 self.assertRaises(UnicodeError, unicode, 'Andr\202 x', 'ascii')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
793 self.assertRaises(UnicodeError, unicode, 'Andr\202 x', 'ascii','strict')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
794 self.assertEqual(unicode('Andr\202 x','ascii','ignore'), u"Andr x")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
795 self.assertEqual(unicode('Andr\202 x','ascii','replace'), u'Andr\uFFFD x')
56093
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
796 self.assertEqual(u'abcde'.decode('ascii', 'ignore'),
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
797 u'abcde'.decode('ascii', errors='ignore'))
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
798 self.assertEqual(u'abcde'.decode('ascii', 'replace'),
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
799 u'abcde'.decode(encoding='ascii', errors='replace'))
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
800
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
801 # Error handling (unknown character names)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
802 self.assertEqual("\\N{foo}xx".decode("unicode-escape", "ignore"), u"xx")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
803
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
804 # Error handling (truncated escape sequence)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
805 self.assertRaises(UnicodeError, "\\".decode, "unicode-escape")
12051
d89556c04ef9 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
806
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
807 self.assertRaises(TypeError, "hello".decode, "test.unicode1")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
808 self.assertRaises(TypeError, unicode, "hello", "test.unicode2")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
809 self.assertRaises(TypeError, u"hello".encode, "test.unicode1")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
810 self.assertRaises(TypeError, u"hello".encode, "test.unicode2")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
811 # executes PyUnicode_Encode()
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
812 import imp
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
813 self.assertRaises(
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
814 ImportError,
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
815 imp.find_module,
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
816 "non-existing module",
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
817 [u"non-existing dir"]
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
818 )
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
819
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
820 # Error handling (wrong arguments)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
821 self.assertRaises(TypeError, u"hello".encode, 42, 42, 42)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
822
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
823 # Error handling (PyUnicode_EncodeDecimal())
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
824 self.assertRaises(UnicodeError, int, u"\u0200")
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
825
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
826 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
827 # Encoding
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
828 self.assertEqual(u'hello'.encode('ascii'), 'hello')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
829 self.assertEqual(u'hello'.encode('utf-7'), 'hello')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
830 self.assertEqual(u'hello'.encode('utf-8'), 'hello')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
831 self.assertEqual(u'hello'.encode('utf8'), 'hello')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
832 self.assertEqual(u'hello'.encode('utf-16-le'), 'h\000e\000l\000l\000o\000')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
833 self.assertEqual(u'hello'.encode('utf-16-be'), '\000h\000e\000l\000l\000o')
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
834 self.assertEqual(u'hello'.encode('latin-1'), '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
835
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
836 # Roundtrip safety for BMP (just the first 1024 chars)
36056
f1ae11457d34 Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents: 35938
diff changeset
837 for c in xrange(1024):
f1ae11457d34 Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents: 35938
diff changeset
838 u = unichr(c)
f1ae11457d34 Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents: 35938
diff changeset
839 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
840 '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
841 'unicode_escape', 'unicode_internal'):
f1ae11457d34 Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents: 35938
diff changeset
842 self.assertEqual(unicode(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
843
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
844 # Roundtrip safety for BMP (just the first 256 chars)
36056
f1ae11457d34 Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents: 35938
diff changeset
845 for c in xrange(256):
f1ae11457d34 Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents: 35938
diff changeset
846 u = unichr(c)
f1ae11457d34 Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents: 35938
diff changeset
847 for encoding in ('latin-1',):
f1ae11457d34 Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents: 35938
diff changeset
848 self.assertEqual(unicode(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
849
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
850 # Roundtrip safety for BMP (just the first 128 chars)
36056
f1ae11457d34 Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents: 35938
diff changeset
851 for c in xrange(128):
f1ae11457d34 Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents: 35938
diff changeset
852 u = unichr(c)
f1ae11457d34 Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents: 35938
diff changeset
853 for encoding in ('ascii',):
f1ae11457d34 Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents: 35938
diff changeset
854 self.assertEqual(unicode(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
855
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
856 # Roundtrip safety for non-BMP (just a few chars)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
857 u = u'\U00010001\U00020002\U00030003\U00040004\U00050005'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
858 for encoding in ('utf-8', 'utf-16', 'utf-16-le', 'utf-16-be',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
859 #'raw_unicode_escape',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
860 'unicode_escape', 'unicode_internal'):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
861 self.assertEqual(unicode(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
862
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
863 # UTF-8 must be roundtrip safe for all UCS-2 code points
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
864 # This excludes surrogates: in the full range, there would be
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
865 # a surrogate pair (\udbff\udc00), which gets converted back
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
866 # to a non-BMP character (\U0010fc00)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
867 u = u''.join(map(unichr, range(0,0xd800)+range(0xe000,0x10000)))
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
868 for encoding in ('utf-8',):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
869 self.assertEqual(unicode(u.encode(encoding),encoding), u)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
870
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
871 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
872 # 0-127
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
873 s = ''.join(map(chr, xrange(128)))
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
874 for encoding in (
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
875 'cp037', 'cp1026',
61884
c56fb63c648f #8016: add the CP858 codec (approved by Benjamin). (Also add CP720 to the tests, it was missing there.)
Georg Brandl <georg@python.org>
parents: 59897
diff changeset
876 'cp437', 'cp500', 'cp720', 'cp737', 'cp775', 'cp850',
c56fb63c648f #8016: add the CP858 codec (approved by Benjamin). (Also add CP720 to the tests, it was missing there.)
Georg Brandl <georg@python.org>
parents: 59897
diff changeset
877 'cp852', 'cp855', 'cp858', 'cp860', 'cp861', 'cp862',
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
878 'cp863', 'cp865', 'cp866',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
879 '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
880 '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
881 '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
882 '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
883
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
884 '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
885 '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
886 '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
887
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
888 '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
889 '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
890
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
891 ### 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
892 #'cp424',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
893
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
894 ### 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
895 #'cp875'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
896
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
897 ):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
898 self.assertEqual(unicode(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
899
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
900 # 128-255
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
901 s = ''.join(map(chr, xrange(128, 256)))
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
902 for encoding in (
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
903 'cp037', 'cp1026',
61884
c56fb63c648f #8016: add the CP858 codec (approved by Benjamin). (Also add CP720 to the tests, it was missing there.)
Georg Brandl <georg@python.org>
parents: 59897
diff changeset
904 'cp437', 'cp500', 'cp720', 'cp737', 'cp775', 'cp850',
c56fb63c648f #8016: add the CP858 codec (approved by Benjamin). (Also add CP720 to the tests, it was missing there.)
Georg Brandl <georg@python.org>
parents: 59897
diff changeset
905 'cp852', 'cp855', 'cp858', 'cp860', 'cp861', 'cp862',
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
906 'cp863', 'cp865', 'cp866',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
907 '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
908 '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
909 '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
910 '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
911
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
912 ### 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
913 #'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
914 #'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
915 #'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
916 #'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
917 #'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
918
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
919 ### 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
920 #'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
921
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
922 ):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
923 self.assertEqual(unicode(s, encoding).encode(encoding), s)
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
924
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
925 def test_concatenation(self):
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
926 self.assertEqual((u"abc" u"def"), u"abcdef")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
927 self.assertEqual(("abc" u"def"), u"abcdef")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
928 self.assertEqual((u"abc" "def"), u"abcdef")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
929 self.assertEqual((u"abc" u"def" "ghi"), u"abcdefghi")
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
930 self.assertEqual(("abc" "def" u"ghi"), u"abcdefghi")
21027
dee0e866653e Fix for bug #480188: printing unicode objects
Marc-André Lemburg <mal@egenix.com>
parents: 20563
diff changeset
931
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
932 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
933 class BitBucket:
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
934 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
935 pass
24598
12e146171f3d Committing patch #591250 which provides "str1 in str2" when str1 is a
Barry Warsaw <barry@python.org>
parents: 24538
diff changeset
936
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
937 out = BitBucket()
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
938 print >>out, u'abc'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
939 print >>out, u'abc', u'def'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
940 print >>out, u'abc', 'def'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
941 print >>out, 'abc', u'def'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
942 print >>out, u'abc\n'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
943 print >>out, u'abc\n',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
944 print >>out, u'abc\n',
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
945 print >>out, u'def\n'
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
946 print >>out, u'def\n'
24598
12e146171f3d Committing patch #591250 which provides "str1 in str2" when str1 is a
Barry Warsaw <barry@python.org>
parents: 24538
diff changeset
947
29164
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28917
diff changeset
948 def test_ucs4(self):
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28917
diff changeset
949 x = u'\U00100000'
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28917
diff changeset
950 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
951 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
952
46028
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
953 y = r'\U00100000'
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
954 x = y.decode("raw-unicode-escape").encode("raw-unicode-escape")
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
955 self.assertEqual(x, y)
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
956 y = r'\U00010000'
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
957 x = y.decode("raw-unicode-escape").encode("raw-unicode-escape")
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
958 self.assertEqual(x, y)
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
959
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
960 try:
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
961 '\U11111111'.decode("raw-unicode-escape")
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
962 except UnicodeDecodeError as e:
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
963 self.assertEqual(e.start, 0)
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
964 self.assertEqual(e.end, 10)
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
965 else:
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
966 self.fail("Should have raised UnicodeDecodeError")
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
967
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
968 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
969 # 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
970 class Foo0:
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
971 def __str__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
972 return "foo"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
973
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
974 class Foo1:
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
975 def __unicode__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
976 return u"foo"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
977
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
978 class Foo2(object):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
979 def __unicode__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
980 return u"foo"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
981
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
982 class Foo3(object):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
983 def __unicode__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
984 return "foo"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
985
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
986 class Foo4(str):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
987 def __unicode__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
988 return "foo"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
989
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
990 class Foo5(unicode):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
991 def __unicode__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
992 return "foo"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
993
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
994 class Foo6(str):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
995 def __str__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
996 return "foos"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
997
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
998 def __unicode__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
999 return u"foou"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1000
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1001 class Foo7(unicode):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1002 def __str__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1003 return "foos"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1004 def __unicode__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1005 return u"foou"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1006
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1007 class Foo8(unicode):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1008 def __new__(cls, content=""):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1009 return unicode.__new__(cls, 2*content)
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1010 def __unicode__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1011 return self
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1012
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1013 class Foo9(unicode):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1014 def __str__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1015 return "string"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1016 def __unicode__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1017 return "not unicode"
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1018
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1019 self.assertEqual(unicode(Foo0()), u"foo")
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1020 self.assertEqual(unicode(Foo1()), u"foo")
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1021 self.assertEqual(unicode(Foo2()), u"foo")
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1022 self.assertEqual(unicode(Foo3()), u"foo")
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1023 self.assertEqual(unicode(Foo4("bar")), u"foo")
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1024 self.assertEqual(unicode(Foo5("bar")), u"foo")
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1025 self.assertEqual(unicode(Foo6("bar")), u"foou")
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1026 self.assertEqual(unicode(Foo7("bar")), u"foou")
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1027 self.assertEqual(unicode(Foo8("foo")), u"foofoo")
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1028 self.assertEqual(str(Foo9("foo")), "string")
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1029 self.assertEqual(unicode(Foo9("foo")), u"not unicode")
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33298
diff changeset
1030
37301
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1031 def test_unicode_repr(self):
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1032 class s1:
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1033 def __repr__(self):
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1034 return '\\n'
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1035
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1036 class s2:
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1037 def __repr__(self):
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1038 return u'\\n'
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1039
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1040 self.assertEqual(repr(s1()), '\\n')
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1041 self.assertEqual(repr(s2()), '\\n')
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1042
42004
e62b606eff0b Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents: 41534
diff changeset
1043 def test_expandtabs_overflows_gracefully(self):
e62b606eff0b Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents: 41534
diff changeset
1044 # This test only affects 32-bit platforms because expandtabs can only take
e62b606eff0b Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents: 41534
diff changeset
1045 # an int as the max value, not a 64-bit C long. If expandtabs is changed
e62b606eff0b Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents: 41534
diff changeset
1046 # to take a 64-bit long, this test should apply to all platforms.
42023
dde7c37f4536 Prevent these tests from running on Win64 since they don\'t apply there either
Neal Norwitz <nnorwitz@gmail.com>
parents: 42004
diff changeset
1047 if sys.maxint > (1 << 32) or struct.calcsize('P') != 4:
42004
e62b606eff0b Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents: 41534
diff changeset
1048 return
e62b606eff0b Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents: 41534
diff changeset
1049 self.assertRaises(OverflowError, u't\tt\t'.expandtabs, sys.maxint)
37301
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1050
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1051 def test__format__(self):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1052 def test(value, format, expected):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1053 # test both with and without the trailing 's'
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1054 self.assertEqual(value.__format__(format), expected)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1055 self.assertEqual(value.__format__(format + u's'), expected)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1056
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1057 test(u'', u'', u'')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1058 test(u'abc', u'', u'abc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1059 test(u'abc', u'.3', u'abc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1060 test(u'ab', u'.3', u'ab')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1061 test(u'abcdef', u'.3', u'abc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1062 test(u'abcdef', u'.0', u'')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1063 test(u'abc', u'3.3', u'abc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1064 test(u'abc', u'2.3', u'abc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1065 test(u'abc', u'2.2', u'ab')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1066 test(u'abc', u'3.2', u'ab ')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1067 test(u'result', u'x<0', u'result')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1068 test(u'result', u'x<5', u'result')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1069 test(u'result', u'x<6', u'result')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1070 test(u'result', u'x<7', u'resultx')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1071 test(u'result', u'x<8', u'resultxx')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1072 test(u'result', u' <7', u'result ')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1073 test(u'result', u'<7', u'result ')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1074 test(u'result', u'>7', u' result')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1075 test(u'result', u'>8', u' result')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1076 test(u'result', u'^8', u' result ')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1077 test(u'result', u'^9', u' result ')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1078 test(u'result', u'^10', u' result ')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1079 test(u'a', u'10000', u'a' + u' ' * 9999)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1080 test(u'', u'10000', u' ' * 10000)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1081 test(u'', u'10000000', u' ' * 10000000)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1082
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1083 # test mixing unicode and str
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1084 self.assertEqual(u'abc'.__format__('s'), u'abc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1085 self.assertEqual(u'abc'.__format__('->10s'), u'-------abc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1086
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1087 def test_format(self):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1088 self.assertEqual(u''.format(), u'')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1089 self.assertEqual(u'a'.format(), u'a')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1090 self.assertEqual(u'ab'.format(), u'ab')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1091 self.assertEqual(u'a{{'.format(), u'a{')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1092 self.assertEqual(u'a}}'.format(), u'a}')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1093 self.assertEqual(u'{{b'.format(), u'{b')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1094 self.assertEqual(u'}}b'.format(), u'}b')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1095 self.assertEqual(u'a{{b'.format(), u'a{b')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1096
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1097 # examples from the PEP:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1098 import datetime
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1099 self.assertEqual(u"My name is {0}".format(u'Fred'), u"My name is Fred")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1100 self.assertEqual(u"My name is {0[name]}".format(dict(name=u'Fred')),
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1101 u"My name is Fred")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1102 self.assertEqual(u"My name is {0} :-{{}}".format(u'Fred'),
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1103 u"My name is Fred :-{}")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1104
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1105 # datetime.__format__ doesn't work with unicode
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1106 #d = datetime.date(2007, 8, 18)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1107 #self.assertEqual("The year is {0.year}".format(d),
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1108 # "The year is 2007")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1109
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1110 # classes we'll use for testing
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1111 class C:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1112 def __init__(self, x=100):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1113 self._x = x
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1114 def __format__(self, spec):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1115 return spec
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1116
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1117 class D:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1118 def __init__(self, x):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1119 self.x = x
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1120 def __format__(self, spec):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1121 return str(self.x)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1122
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1123 # class with __str__, but no __format__
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1124 class E:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1125 def __init__(self, x):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1126 self.x = x
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1127 def __str__(self):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1128 return u'E(' + self.x + u')'
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1129
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1130 # class with __repr__, but no __format__ or __str__
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1131 class F:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1132 def __init__(self, x):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1133 self.x = x
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1134 def __repr__(self):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1135 return u'F(' + self.x + u')'
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1136
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1137 # class with __format__ that forwards to string, for some format_spec's
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1138 class G:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1139 def __init__(self, x):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1140 self.x = x
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1141 def __str__(self):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1142 return u"string is " + self.x
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1143 def __format__(self, format_spec):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1144 if format_spec == 'd':
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1145 return u'G(' + self.x + u')'
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1146 return object.__format__(self, format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1147
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1148 # class that returns a bad type from __format__
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1149 class H:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1150 def __format__(self, format_spec):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1151 return 1.0
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1152
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1153 class I(datetime.date):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1154 def __format__(self, format_spec):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1155 return self.strftime(format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1156
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1157 class J(int):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1158 def __format__(self, format_spec):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1159 return int.__format__(self * 2, format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1160
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1161
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1162 self.assertEqual(u''.format(), u'')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1163 self.assertEqual(u'abc'.format(), u'abc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1164 self.assertEqual(u'{0}'.format(u'abc'), u'abc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1165 self.assertEqual(u'{0:}'.format(u'abc'), u'abc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1166 self.assertEqual(u'X{0}'.format(u'abc'), u'Xabc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1167 self.assertEqual(u'{0}X'.format(u'abc'), u'abcX')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1168 self.assertEqual(u'X{0}Y'.format(u'abc'), u'XabcY')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1169 self.assertEqual(u'{1}'.format(1, u'abc'), u'abc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1170 self.assertEqual(u'X{1}'.format(1, u'abc'), u'Xabc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1171 self.assertEqual(u'{1}X'.format(1, u'abc'), u'abcX')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1172 self.assertEqual(u'X{1}Y'.format(1, u'abc'), u'XabcY')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1173 self.assertEqual(u'{0}'.format(-15), u'-15')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1174 self.assertEqual(u'{0}{1}'.format(-15, u'abc'), u'-15abc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1175 self.assertEqual(u'{0}X{1}'.format(-15, u'abc'), u'-15Xabc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1176 self.assertEqual(u'{{'.format(), u'{')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1177 self.assertEqual(u'}}'.format(), u'}')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1178 self.assertEqual(u'{{}}'.format(), u'{}')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1179 self.assertEqual(u'{{x}}'.format(), u'{x}')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1180 self.assertEqual(u'{{{0}}}'.format(123), u'{123}')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1181 self.assertEqual(u'{{{{0}}}}'.format(), u'{{0}}')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1182 self.assertEqual(u'}}{{'.format(), u'}{')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1183 self.assertEqual(u'}}x{{'.format(), u'}x{')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1184
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1185 # weird field names
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1186 self.assertEqual(u"{0[foo-bar]}".format({u'foo-bar':u'baz'}), u'baz')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1187 self.assertEqual(u"{0[foo bar]}".format({u'foo bar':u'baz'}), u'baz')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1188 self.assertEqual(u"{0[ ]}".format({u' ':3}), u'3')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1189
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1190 self.assertEqual(u'{foo._x}'.format(foo=C(20)), u'20')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1191 self.assertEqual(u'{1}{0}'.format(D(10), D(20)), u'2010')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1192 self.assertEqual(u'{0._x.x}'.format(C(D(u'abc'))), u'abc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1193 self.assertEqual(u'{0[0]}'.format([u'abc', u'def']), u'abc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1194 self.assertEqual(u'{0[1]}'.format([u'abc', u'def']), u'def')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1195 self.assertEqual(u'{0[1][0]}'.format([u'abc', [u'def']]), u'def')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1196 self.assertEqual(u'{0[1][0].x}'.format(['abc', [D(u'def')]]), u'def')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1197
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1198 # strings
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1199 self.assertEqual(u'{0:.3s}'.format(u'abc'), u'abc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1200 self.assertEqual(u'{0:.3s}'.format(u'ab'), u'ab')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1201 self.assertEqual(u'{0:.3s}'.format(u'abcdef'), u'abc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1202 self.assertEqual(u'{0:.0s}'.format(u'abcdef'), u'')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1203 self.assertEqual(u'{0:3.3s}'.format(u'abc'), u'abc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1204 self.assertEqual(u'{0:2.3s}'.format(u'abc'), u'abc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1205 self.assertEqual(u'{0:2.2s}'.format(u'abc'), u'ab')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1206 self.assertEqual(u'{0:3.2s}'.format(u'abc'), u'ab ')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1207 self.assertEqual(u'{0:x<0s}'.format(u'result'), u'result')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1208 self.assertEqual(u'{0:x<5s}'.format(u'result'), u'result')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1209 self.assertEqual(u'{0:x<6s}'.format(u'result'), u'result')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1210 self.assertEqual(u'{0:x<7s}'.format(u'result'), u'resultx')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1211 self.assertEqual(u'{0:x<8s}'.format(u'result'), u'resultxx')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1212 self.assertEqual(u'{0: <7s}'.format(u'result'), u'result ')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1213 self.assertEqual(u'{0:<7s}'.format(u'result'), u'result ')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1214 self.assertEqual(u'{0:>7s}'.format(u'result'), u' result')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1215 self.assertEqual(u'{0:>8s}'.format(u'result'), u' result')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1216 self.assertEqual(u'{0:^8s}'.format(u'result'), u' result ')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1217 self.assertEqual(u'{0:^9s}'.format(u'result'), u' result ')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1218 self.assertEqual(u'{0:^10s}'.format(u'result'), u' result ')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1219 self.assertEqual(u'{0:10000}'.format(u'a'), u'a' + u' ' * 9999)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1220 self.assertEqual(u'{0:10000}'.format(u''), u' ' * 10000)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1221 self.assertEqual(u'{0:10000000}'.format(u''), u' ' * 10000000)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1222
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1223 # format specifiers for user defined type
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1224 self.assertEqual(u'{0:abc}'.format(C()), u'abc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1225
64707
274a0bf46871 Check PendingDeprecationWarning after issue #7994.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64701
diff changeset
1226 # !r and !s coercions
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1227 self.assertEqual(u'{0!s}'.format(u'Hello'), u'Hello')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1228 self.assertEqual(u'{0!s:}'.format(u'Hello'), u'Hello')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1229 self.assertEqual(u'{0!s:15}'.format(u'Hello'), u'Hello ')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1230 self.assertEqual(u'{0!s:15s}'.format(u'Hello'), u'Hello ')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1231 self.assertEqual(u'{0!r}'.format(u'Hello'), u"u'Hello'")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1232 self.assertEqual(u'{0!r:}'.format(u'Hello'), u"u'Hello'")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1233 self.assertEqual(u'{0!r}'.format(F(u'Hello')), u'F(Hello)')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1234
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1235 # test fallback to object.__format__
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1236 self.assertEqual(u'{0}'.format({}), u'{}')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1237 self.assertEqual(u'{0}'.format([]), u'[]')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1238 self.assertEqual(u'{0}'.format([1]), u'[1]')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1239 self.assertEqual(u'{0}'.format(E(u'data')), u'E(data)')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1240 self.assertEqual(u'{0:d}'.format(G(u'data')), u'G(data)')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1241 self.assertEqual(u'{0!s}'.format(G(u'data')), u'string is data')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1242
64707
274a0bf46871 Check PendingDeprecationWarning after issue #7994.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64701
diff changeset
1243 msg = 'object.__format__ with a non-empty format string is deprecated'
274a0bf46871 Check PendingDeprecationWarning after issue #7994.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64701
diff changeset
1244 with test_support.check_warnings((msg, PendingDeprecationWarning)):
274a0bf46871 Check PendingDeprecationWarning after issue #7994.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64701
diff changeset
1245 self.assertEqual(u'{0:^10}'.format(E(u'data')), u' E(data) ')
274a0bf46871 Check PendingDeprecationWarning after issue #7994.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64701
diff changeset
1246 self.assertEqual(u'{0:^10s}'.format(E(u'data')), u' E(data) ')
274a0bf46871 Check PendingDeprecationWarning after issue #7994.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64701
diff changeset
1247 self.assertEqual(u'{0:>15s}'.format(G(u'data')), u' string is data')
274a0bf46871 Check PendingDeprecationWarning after issue #7994.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64701
diff changeset
1248
52161
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1249 self.assertEqual(u"{0:date: %Y-%m-%d}".format(I(year=2007,
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1250 month=8,
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1251 day=27)),
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1252 u"date: 2007-08-27")
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1253
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1254 # test deriving from a builtin type and overriding __format__
52161
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1255 self.assertEqual(u"{0}".format(J(10)), u"20")
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1256
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1257
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1258 # string format specifiers
52161
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1259 self.assertEqual(u'{0:}'.format('a'), u'a')
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1260
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1261 # computed format specifiers
52161
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1262 self.assertEqual(u"{0:.{1}}".format(u'hello world', 5), u'hello')
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1263 self.assertEqual(u"{0:.{1}s}".format(u'hello world', 5), u'hello')
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1264 self.assertEqual(u"{0:.{precision}s}".format('hello world', precision=5), u'hello')
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1265 self.assertEqual(u"{0:{width}.{precision}s}".format('hello world', width=10, precision=5), u'hello ')
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1266 self.assertEqual(u"{0:{width}.{precision}s}".format('hello world', width='10', precision='5'), u'hello ')
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1267
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1268 # test various errors
52161
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1269 self.assertRaises(ValueError, u'{'.format)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1270 self.assertRaises(ValueError, u'}'.format)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1271 self.assertRaises(ValueError, u'a{'.format)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1272 self.assertRaises(ValueError, u'a}'.format)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1273 self.assertRaises(ValueError, u'{a'.format)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1274 self.assertRaises(ValueError, u'}a'.format)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1275 self.assertRaises(IndexError, u'{0}'.format)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1276 self.assertRaises(IndexError, u'{1}'.format, u'abc')
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1277 self.assertRaises(KeyError, u'{x}'.format)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1278 self.assertRaises(ValueError, u"}{".format)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1279 self.assertRaises(ValueError, u"{".format)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1280 self.assertRaises(ValueError, u"}".format)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1281 self.assertRaises(ValueError, u"abc{0:{}".format)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1282 self.assertRaises(ValueError, u"{0".format)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1283 self.assertRaises(IndexError, u"{0.}".format)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1284 self.assertRaises(ValueError, u"{0.}".format, 0)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1285 self.assertRaises(IndexError, u"{0[}".format)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1286 self.assertRaises(ValueError, u"{0[}".format, [])
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1287 self.assertRaises(KeyError, u"{0]}".format)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1288 self.assertRaises(ValueError, u"{0.[]}".format, 0)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1289 self.assertRaises(ValueError, u"{0..foo}".format, 0)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1290 self.assertRaises(ValueError, u"{0[0}".format, 0)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1291 self.assertRaises(ValueError, u"{0[0:foo}".format, 0)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1292 self.assertRaises(KeyError, u"{c]}".format)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1293 self.assertRaises(ValueError, u"{{ {{{0}}".format, 0)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1294 self.assertRaises(ValueError, u"{0}}".format, 0)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1295 self.assertRaises(KeyError, u"{foo}".format, bar=3)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1296 self.assertRaises(ValueError, u"{0!x}".format, 3)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1297 self.assertRaises(ValueError, u"{0!}".format, 0)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1298 self.assertRaises(ValueError, u"{0!rs}".format, 0)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1299 self.assertRaises(ValueError, u"{!}".format)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1300 self.assertRaises(IndexError, u"{:}".format)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1301 self.assertRaises(IndexError, u"{:s}".format)
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1302 self.assertRaises(IndexError, u"{}".format)
62173
e5f0b27bc178 use unicode literals
Benjamin Peterson <benjamin@python.org>
parents: 62168
diff changeset
1303 big = u"23098475029384702983476098230754973209482573"
e5f0b27bc178 use unicode literals
Benjamin Peterson <benjamin@python.org>
parents: 62168
diff changeset
1304 self.assertRaises(ValueError, (u"{" + big + u"}").format)
e5f0b27bc178 use unicode literals
Benjamin Peterson <benjamin@python.org>
parents: 62168
diff changeset
1305 self.assertRaises(ValueError, (u"{[" + big + u"]}").format, [0])
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1306
54267
73f62731b1e6 Issue 6089: str.format raises SystemError.
Eric Smith <eric@trueblade.com>
parents: 53740
diff changeset
1307 # issue 6089
73f62731b1e6 Issue 6089: str.format raises SystemError.
Eric Smith <eric@trueblade.com>
parents: 53740
diff changeset
1308 self.assertRaises(ValueError, u"{0[0]x}".format, [None])
73f62731b1e6 Issue 6089: str.format raises SystemError.
Eric Smith <eric@trueblade.com>
parents: 53740
diff changeset
1309 self.assertRaises(ValueError, u"{0[0](10)}".format, [None])
73f62731b1e6 Issue 6089: str.format raises SystemError.
Eric Smith <eric@trueblade.com>
parents: 53740
diff changeset
1310
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1311 # can't have a replacement on the field name portion
52161
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1312 self.assertRaises(TypeError, u'{0[{1}]}'.format, u'abcdefg', 4)
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1313
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1314 # exceed maximum recursion depth
52161
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1315 self.assertRaises(ValueError, u"{0:{1:{2}}}".format, u'abc', u's', u'')
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1316 self.assertRaises(ValueError, u"{0:{1:{2:{3:{4:{5:{6}}}}}}}".format,
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1317 0, 1, 2, 3, 4, 5, 6, 7)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1318
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 42310
diff changeset
1319 # string format spec errors
52161
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1320 self.assertRaises(ValueError, u"{0:-s}".format, u'')
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1321 self.assertRaises(ValueError, format, u"", u"-")
1f96d733a743 Unicode format tests weren't actually testing unicode. This was probably due to the original backport from py3k.
Eric Smith <eric@trueblade.com>
parents: 52157
diff changeset
1322 self.assertRaises(ValueError, u"{0:=s}".format, u'')
37301
f284e854fe72 Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36341
diff changeset
1323
45391
3fc586307d66 Added code to correct combining str and unicode in ''.format(). Added test case.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1324 # test combining string and unicode
3fc586307d66 Added code to correct combining str and unicode in ''.format(). Added test case.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1325 self.assertEqual(u"foo{0}".format('bar'), u'foobar')
3fc586307d66 Added code to correct combining str and unicode in ''.format(). Added test case.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1326 # This will try to convert the argument from unicode to str, which
3fc586307d66 Added code to correct combining str and unicode in ''.format(). Added test case.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1327 # will succeed
3fc586307d66 Added code to correct combining str and unicode in ''.format(). Added test case.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1328 self.assertEqual("foo{0}".format(u'bar'), 'foobar')
3fc586307d66 Added code to correct combining str and unicode in ''.format(). Added test case.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1329 # This will try to convert the argument from unicode to str, which
3fc586307d66 Added code to correct combining str and unicode in ''.format(). Added test case.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1330 # will fail
3fc586307d66 Added code to correct combining str and unicode in ''.format(). Added test case.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1331 self.assertRaises(UnicodeEncodeError, "foo{0}".format, u'\u1000bar')
3fc586307d66 Added code to correct combining str and unicode in ''.format(). Added test case.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1332
52157
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1333 def test_format_auto_numbering(self):
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1334 class C:
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1335 def __init__(self, x=100):
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1336 self._x = x
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1337 def __format__(self, spec):
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1338 return spec
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1339
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1340 self.assertEqual(u'{}'.format(10), u'10')
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1341 self.assertEqual(u'{:5}'.format('s'), u's ')
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1342 self.assertEqual(u'{!r}'.format('s'), u"'s'")
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1343 self.assertEqual(u'{._x}'.format(C(10)), u'10')
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1344 self.assertEqual(u'{[1]}'.format([1, 2]), u'2')
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1345 self.assertEqual(u'{[a]}'.format({'a':4, 'b':2}), u'4')
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1346 self.assertEqual(u'a{}b{}c'.format(0, 1), u'a0b1c')
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1347
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1348 self.assertEqual(u'a{:{}}b'.format('x', '^10'), u'a x b')
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1349 self.assertEqual(u'a{:{}x}b'.format(20, '#'), u'a0x14b')
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1350
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1351 # can't mix and match numbering and auto-numbering
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1352 self.assertRaises(ValueError, u'{}{1}'.format, 1, 2)
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1353 self.assertRaises(ValueError, u'{1}{}'.format, 1, 2)
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1354 self.assertRaises(ValueError, u'{:{1}}'.format, 1, 2)
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1355 self.assertRaises(ValueError, u'{0:{}}'.format, 1, 2)
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1356
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1357 # can mix and match auto-numbering and named
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1358 self.assertEqual(u'{f}{}'.format(4, f='test'), u'test4')
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1359 self.assertEqual(u'{}{f}'.format(4, f='test'), u'4test')
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1360 self.assertEqual(u'{:{f}}{g}{}'.format(1, 3, g='g', f=2), u' 1g3')
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1361 self.assertEqual(u'{f:{}}{}{g}'.format(2, 4, f=1, g='g'), u' 14g')
3f18d609ac1b Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents: 49031
diff changeset
1362
48117
a94f136b5283 Correct a crash when two successive unicode allocations fail with a MemoryError:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48070
diff changeset
1363 def test_raiseMemError(self):
a94f136b5283 Correct a crash when two successive unicode allocations fail with a MemoryError:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48070
diff changeset
1364 # Ensure that the freelist contains a consistent object, even
a94f136b5283 Correct a crash when two successive unicode allocations fail with a MemoryError:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48070
diff changeset
1365 # when a string allocation fails with a MemoryError.
a94f136b5283 Correct a crash when two successive unicode allocations fail with a MemoryError:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48070
diff changeset
1366 # This used to crash the interpreter,
a94f136b5283 Correct a crash when two successive unicode allocations fail with a MemoryError:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48070
diff changeset
1367 # or leak references when the number was smaller.
49031
433220a60a8d #3601: test_unicode.test_raiseMemError fails in UCS4
Antoine Pitrou <solipsis@pitrou.net>
parents: 48268
diff changeset
1368 charwidth = 4 if sys.maxunicode >= 0x10000 else 2
433220a60a8d #3601: test_unicode.test_raiseMemError fails in UCS4
Antoine Pitrou <solipsis@pitrou.net>
parents: 48268
diff changeset
1369 # Note: sys.maxsize is half of the actual max allocation because of
433220a60a8d #3601: test_unicode.test_raiseMemError fails in UCS4
Antoine Pitrou <solipsis@pitrou.net>
parents: 48268
diff changeset
1370 # the signedness of Py_ssize_t.
433220a60a8d #3601: test_unicode.test_raiseMemError fails in UCS4
Antoine Pitrou <solipsis@pitrou.net>
parents: 48268
diff changeset
1371 alloc = lambda: u"a" * (sys.maxsize // charwidth * 2)
48268
d7d7139d5840 #3556: test_raiseMemError consumes an insane amount of memory
Antoine Pitrou <solipsis@pitrou.net>
parents: 48117
diff changeset
1372 self.assertRaises(MemoryError, alloc)
d7d7139d5840 #3556: test_raiseMemError consumes an insane amount of memory
Antoine Pitrou <solipsis@pitrou.net>
parents: 48117
diff changeset
1373 self.assertRaises(MemoryError, alloc)
48117
a94f136b5283 Correct a crash when two successive unicode allocations fail with a MemoryError:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48070
diff changeset
1374
59895
52cf7e65dad1 Issue #1583863: An unicode subclass can now override the __str__ method
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59432
diff changeset
1375 def test_format_subclass(self):
52cf7e65dad1 Issue #1583863: An unicode subclass can now override the __str__ method
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59432
diff changeset
1376 class U(unicode):
52cf7e65dad1 Issue #1583863: An unicode subclass can now override the __str__ method
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59432
diff changeset
1377 def __unicode__(self):
52cf7e65dad1 Issue #1583863: An unicode subclass can now override the __str__ method
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59432
diff changeset
1378 return u'__unicode__ overridden'
52cf7e65dad1 Issue #1583863: An unicode subclass can now override the __str__ method
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59432
diff changeset
1379 u = U(u'xxx')
64701
28e6a41c5ba0 Merged revisions 84470-84471,84566-84567,84759 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63102
diff changeset
1380 self.assertEqual("%s" % u, u'__unicode__ overridden')
64708
44199aa8932b Strengthen test_unicode with explicit type checking for assertEqual tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64707
diff changeset
1381 self.assertEqual("{}".format(u), '__unicode__ overridden')
59895
52cf7e65dad1 Issue #1583863: An unicode subclass can now override the __str__ method
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59432
diff changeset
1382
52cf7e65dad1 Issue #1583863: An unicode subclass can now override the __str__ method
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59432
diff changeset
1383
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1384 def test_main():
41534
6c4d7678af5e Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule().
Collin Winter <collinw@gmail.com>
parents: 39621
diff changeset
1385 test_support.run_unittest(__name__)
27304
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1386
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1387 if __name__ == "__main__":
a555230be3cb Port test_unicode.py to PyUnit and add tests for error
Walter Dörwald <walter@livinglogic.de>
parents: 27176
diff changeset
1388 test_main()