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

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 499a96611baa
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
1 """Unit tests for the bytes and bytearray types.
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
2
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
3 XXX This is a mess. Common tests should be moved to buffer_tests.py,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
4 which itself ought to be unified with string_tests.py (and the latter
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
5 should be modernized).
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
6 """
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
8 import os
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
9 import re
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 import sys
43757
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
11 import copy
64408
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
12 import functools
41425
0486deaaade3 An example of action-at-a-distance: fix the problems I had in test_io.py
Guido van Rossum <guido@python.org>
parents: 41412
diff changeset
13 import pickle
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
14 import tempfile
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 import unittest
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45058
diff changeset
16 import test.support
41038
efa0a761918c Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents: 40978
diff changeset
17 import test.string_tests
43576
697cc31a83f6 For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents: 43508
diff changeset
18 import test.buffer_tests
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
64408
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
20
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
21 if sys.flags.bytes_warning:
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
22 def check_bytes_warnings(func):
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
23 @functools.wraps(func)
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
24 def wrapper(*args, **kw):
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
25 with test.support.check_warnings(('', BytesWarning)):
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
26 return func(*args, **kw)
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
27 return wrapper
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
28 else:
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
29 # no-op
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
30 def check_bytes_warnings(func):
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
31 return func
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
32
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
33
48508
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
34 class Indexable:
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
35 def __init__(self, value=0):
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
36 self.value = value
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
37 def __index__(self):
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
38 return self.value
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
39
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
81356
55383912c225 #16910: test_bytes, test_unicode, and test_userstring now work with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80954
diff changeset
41 class BaseBytesTest:
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 def test_basics(self):
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
44 b = self.type2test()
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
45 self.assertEqual(type(b), self.type2test)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
46 self.assertEqual(b.__class__, self.type2test)
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
58264
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
48 def test_copy(self):
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
49 a = self.type2test(b"abcd")
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
50 for copy_method in (copy.copy, copy.deepcopy):
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
51 b = copy_method(a)
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
52 self.assertEqual(a, b)
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
53 self.assertEqual(type(a), type(b))
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
54
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 def test_empty_sequence(self):
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
56 b = self.type2test()
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 self.assertEqual(len(b), 0)
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 self.assertRaises(IndexError, lambda: b[0])
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 self.assertRaises(IndexError, lambda: b[1])
44160
9d152530910a Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents: 44137
diff changeset
60 self.assertRaises(IndexError, lambda: b[sys.maxsize])
9d152530910a Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents: 44137
diff changeset
61 self.assertRaises(IndexError, lambda: b[sys.maxsize+1])
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 self.assertRaises(IndexError, lambda: b[10**100])
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 self.assertRaises(IndexError, lambda: b[-1])
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 self.assertRaises(IndexError, lambda: b[-2])
44160
9d152530910a Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents: 44137
diff changeset
65 self.assertRaises(IndexError, lambda: b[-sys.maxsize])
9d152530910a Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents: 44137
diff changeset
66 self.assertRaises(IndexError, lambda: b[-sys.maxsize-1])
9d152530910a Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents: 44137
diff changeset
67 self.assertRaises(IndexError, lambda: b[-sys.maxsize-2])
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 self.assertRaises(IndexError, lambda: b[-10**100])
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 def test_from_list(self):
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 ints = list(range(256))
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
72 b = self.type2test(i for i in ints)
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 self.assertEqual(len(b), 256)
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 self.assertEqual(list(b), ints)
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 def test_from_index(self):
48508
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
77 b = self.type2test([Indexable(), Indexable(1), Indexable(254),
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
78 Indexable(255)])
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 self.assertEqual(list(b), [0, 1, 254, 255])
60644
5e1dbc3ae767 Merged revisions 80125,80128,80130 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58564
diff changeset
80 self.assertRaises(ValueError, self.type2test, [Indexable(-1)])
5e1dbc3ae767 Merged revisions 80125,80128,80130 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58564
diff changeset
81 self.assertRaises(ValueError, self.type2test, [Indexable(256)])
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
82
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
83 def test_from_ssize(self):
60644
5e1dbc3ae767 Merged revisions 80125,80128,80130 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58564
diff changeset
84 self.assertEqual(self.type2test(0), b'')
5e1dbc3ae767 Merged revisions 80125,80128,80130 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58564
diff changeset
85 self.assertEqual(self.type2test(1), b'\x00')
5e1dbc3ae767 Merged revisions 80125,80128,80130 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58564
diff changeset
86 self.assertEqual(self.type2test(5), b'\x00\x00\x00\x00\x00')
5e1dbc3ae767 Merged revisions 80125,80128,80130 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58564
diff changeset
87 self.assertRaises(ValueError, self.type2test, -1)
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
88
60644
5e1dbc3ae767 Merged revisions 80125,80128,80130 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58564
diff changeset
89 self.assertEqual(self.type2test('0', 'ascii'), b'0')
5e1dbc3ae767 Merged revisions 80125,80128,80130 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58564
diff changeset
90 self.assertEqual(self.type2test(b'0'), b'0')
5e1dbc3ae767 Merged revisions 80125,80128,80130 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58564
diff changeset
91 self.assertRaises(OverflowError, self.type2test, sys.maxsize + 1)
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 def test_constructor_type_errors(self):
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
94 self.assertRaises(TypeError, self.type2test, 0.0)
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 class C:
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 pass
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
97 self.assertRaises(TypeError, self.type2test, ["0"])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
98 self.assertRaises(TypeError, self.type2test, [0.0])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
99 self.assertRaises(TypeError, self.type2test, [None])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
100 self.assertRaises(TypeError, self.type2test, [C()])
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 def test_constructor_value_errors(self):
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
103 self.assertRaises(ValueError, self.type2test, [-1])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
104 self.assertRaises(ValueError, self.type2test, [-sys.maxsize])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
105 self.assertRaises(ValueError, self.type2test, [-sys.maxsize-1])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
106 self.assertRaises(ValueError, self.type2test, [-sys.maxsize-2])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
107 self.assertRaises(ValueError, self.type2test, [-10**100])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
108 self.assertRaises(ValueError, self.type2test, [256])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
109 self.assertRaises(ValueError, self.type2test, [257])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
110 self.assertRaises(ValueError, self.type2test, [sys.maxsize])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
111 self.assertRaises(ValueError, self.type2test, [sys.maxsize+1])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
112 self.assertRaises(ValueError, self.type2test, [10**100])
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 def test_compare(self):
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
115 b1 = self.type2test([1, 2, 3])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
116 b2 = self.type2test([1, 2, 3])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
117 b3 = self.type2test([1, 3])
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
42188
81930eaae715 Patch by Christian Heimes to change self.assert_(x == y) into
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
119 self.assertEqual(b1, b2)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
120 self.assertTrue(b2 != b3)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
121 self.assertTrue(b1 <= b2)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
122 self.assertTrue(b1 <= b3)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
123 self.assertTrue(b1 < b3)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
124 self.assertTrue(b1 >= b2)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
125 self.assertTrue(b3 >= b2)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
126 self.assertTrue(b3 > b2)
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
128 self.assertFalse(b1 != b2)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
129 self.assertFalse(b2 == b3)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
130 self.assertFalse(b1 > b2)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
131 self.assertFalse(b1 > b3)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
132 self.assertFalse(b1 >= b3)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
133 self.assertFalse(b1 < b2)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
134 self.assertFalse(b3 < b2)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
135 self.assertFalse(b3 <= b2)
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
64408
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
137 @check_bytes_warnings
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
138 def test_compare_to_str(self):
43508
47c02791ec6b Patch #1049 by Thomas Lee.
Guido van Rossum <guido@python.org>
parents: 43487
diff changeset
139 # Byte comparisons with unicode should always fail!
64408
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
140 # Test this for all expected byte orders and Unicode character
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
141 # sizes.
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
142 self.assertEqual(self.type2test(b"\0a\0b\0c") == "abc", False)
64408
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
143 self.assertEqual(self.type2test(b"\0\0\0a\0\0\0b\0\0\0c") == "abc",
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
144 False)
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
145 self.assertEqual(self.type2test(b"a\0b\0c\0") == "abc", False)
64408
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
146 self.assertEqual(self.type2test(b"a\0\0\0b\0\0\0c\0\0\0") == "abc",
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
147 False)
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
148 self.assertEqual(self.type2test() == str(), False)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
149 self.assertEqual(self.type2test() != str(), True)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
150
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
151 def test_reversed(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
152 input = list(map(ord, "Hello"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
153 b = self.type2test(input)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
154 output = list(reversed(b))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
155 input.reverse()
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
156 self.assertEqual(output, input)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
157
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
158 def test_getslice(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
159 def by(s):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
160 return self.type2test(map(ord, s))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
161 b = by("Hello, world")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
162
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
163 self.assertEqual(b[:5], by("Hello"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
164 self.assertEqual(b[1:5], by("ello"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
165 self.assertEqual(b[5:7], by(", "))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
166 self.assertEqual(b[7:], by("world"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
167 self.assertEqual(b[7:12], by("world"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
168 self.assertEqual(b[7:100], by("world"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
169
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
170 self.assertEqual(b[:-7], by("Hello"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
171 self.assertEqual(b[-11:-7], by("ello"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
172 self.assertEqual(b[-7:-5], by(", "))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
173 self.assertEqual(b[-5:], by("world"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
174 self.assertEqual(b[-5:12], by("world"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
175 self.assertEqual(b[-5:100], by("world"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
176 self.assertEqual(b[-100:5], by("Hello"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
177
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
178 def test_extended_getslice(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
179 # Test extended slicing by comparing with list slicing.
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
180 L = list(range(255))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
181 b = self.type2test(L)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
182 indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
183 for start in indices:
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
184 for stop in indices:
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
185 # Skip step 0 (invalid)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
186 for step in indices[1:]:
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
187 self.assertEqual(b[start:stop:step], self.type2test(L[start:stop:step]))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
188
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
189 def test_encoding(self):
53681
52a77ef069cd Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents: 53094
diff changeset
190 sample = "Hello world\n\u1234\u5678\u9abc"
68142
be6c38d1817b Normalize the encoding names for Latin-1 and UTF-8 to
Marc-André Lemburg <mal@egenix.com>
parents: 67368
diff changeset
191 for enc in ("utf-8", "utf-16"):
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
192 b = self.type2test(sample, enc)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
193 self.assertEqual(b, self.type2test(sample.encode(enc)))
68142
be6c38d1817b Normalize the encoding names for Latin-1 and UTF-8 to
Marc-André Lemburg <mal@egenix.com>
parents: 67368
diff changeset
194 self.assertRaises(UnicodeEncodeError, self.type2test, sample, "latin-1")
be6c38d1817b Normalize the encoding names for Latin-1 and UTF-8 to
Marc-André Lemburg <mal@egenix.com>
parents: 67368
diff changeset
195 b = self.type2test(sample, "latin-1", "ignore")
53681
52a77ef069cd Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents: 53094
diff changeset
196 self.assertEqual(b, self.type2test(sample[:-3], "utf-8"))
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
197
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
198 def test_decode(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
199 sample = "Hello world\n\u1234\u5678\u9abc\def0\def0"
68142
be6c38d1817b Normalize the encoding names for Latin-1 and UTF-8 to
Marc-André Lemburg <mal@egenix.com>
parents: 67368
diff changeset
200 for enc in ("utf-8", "utf-16"):
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
201 b = self.type2test(sample, enc)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
202 self.assertEqual(b.decode(enc), sample)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
203 sample = "Hello world\n\x80\x81\xfe\xff"
68142
be6c38d1817b Normalize the encoding names for Latin-1 and UTF-8 to
Marc-André Lemburg <mal@egenix.com>
parents: 67368
diff changeset
204 b = self.type2test(sample, "latin-1")
be6c38d1817b Normalize the encoding names for Latin-1 and UTF-8 to
Marc-André Lemburg <mal@egenix.com>
parents: 67368
diff changeset
205 self.assertRaises(UnicodeDecodeError, b.decode, "utf-8")
be6c38d1817b Normalize the encoding names for Latin-1 and UTF-8 to
Marc-André Lemburg <mal@egenix.com>
parents: 67368
diff changeset
206 self.assertEqual(b.decode("utf-8", "ignore"), "Hello world\n")
be6c38d1817b Normalize the encoding names for Latin-1 and UTF-8 to
Marc-André Lemburg <mal@egenix.com>
parents: 67368
diff changeset
207 self.assertEqual(b.decode(errors="ignore", encoding="utf-8"),
56099
1b7bdae02179 Merged revisions 74929 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55848
diff changeset
208 "Hello world\n")
68142
be6c38d1817b Normalize the encoding names for Latin-1 and UTF-8 to
Marc-André Lemburg <mal@egenix.com>
parents: 67368
diff changeset
209 # Default encoding is utf-8
be6c38d1817b Normalize the encoding names for Latin-1 and UTF-8 to
Marc-André Lemburg <mal@egenix.com>
parents: 67368
diff changeset
210 self.assertEqual(self.type2test(b'\xe2\x98\x83').decode(), '\u2603')
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
211
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
212 def test_from_int(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
213 b = self.type2test(0)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
214 self.assertEqual(b, self.type2test())
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
215 b = self.type2test(10)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
216 self.assertEqual(b, self.type2test([0]*10))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
217 b = self.type2test(10000)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
218 self.assertEqual(b, self.type2test([0]*10000))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
219
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
220 def test_concat(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
221 b1 = self.type2test(b"abc")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
222 b2 = self.type2test(b"def")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
223 self.assertEqual(b1 + b2, b"abcdef")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
224 self.assertEqual(b1 + bytes(b"def"), b"abcdef")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
225 self.assertEqual(bytes(b"def") + b1, b"defabc")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
226 self.assertRaises(TypeError, lambda: b1 + "def")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
227 self.assertRaises(TypeError, lambda: "abc" + b2)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
228
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
229 def test_repeat(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
230 for b in b"abc", self.type2test(b"abc"):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
231 self.assertEqual(b * 3, b"abcabcabc")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
232 self.assertEqual(b * 0, b"")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
233 self.assertEqual(b * -1, b"")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
234 self.assertRaises(TypeError, lambda: b * 3.14)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
235 self.assertRaises(TypeError, lambda: 3.14 * b)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
236 # XXX Shouldn't bytes and bytearray agree on what to raise?
63913
7dd5f5cfa179 Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents: 60856
diff changeset
237 with self.assertRaises((OverflowError, MemoryError)):
7dd5f5cfa179 Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents: 60856
diff changeset
238 c = b * sys.maxsize
7dd5f5cfa179 Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents: 60856
diff changeset
239 with self.assertRaises((OverflowError, MemoryError)):
7dd5f5cfa179 Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents: 60856
diff changeset
240 b *= sys.maxsize
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
241
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
242 def test_repeat_1char(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
243 self.assertEqual(self.type2test(b'x')*100, self.type2test([ord('x')]*100))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
244
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
245 def test_contains(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
246 b = self.type2test(b"abc")
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58388
diff changeset
247 self.assertIn(ord('a'), b)
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58388
diff changeset
248 self.assertIn(int(ord('a')), b)
58456
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
249 self.assertNotIn(200, b)
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
250 self.assertRaises(ValueError, lambda: 300 in b)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
251 self.assertRaises(ValueError, lambda: -1 in b)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
252 self.assertRaises(TypeError, lambda: None in b)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
253 self.assertRaises(TypeError, lambda: float(ord('a')) in b)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
254 self.assertRaises(TypeError, lambda: "a" in b)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
255 for f in bytes, bytearray:
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58388
diff changeset
256 self.assertIn(f(b""), b)
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58388
diff changeset
257 self.assertIn(f(b"a"), b)
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58388
diff changeset
258 self.assertIn(f(b"b"), b)
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58388
diff changeset
259 self.assertIn(f(b"c"), b)
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58388
diff changeset
260 self.assertIn(f(b"ab"), b)
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58388
diff changeset
261 self.assertIn(f(b"bc"), b)
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58388
diff changeset
262 self.assertIn(f(b"abc"), b)
58456
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
263 self.assertNotIn(f(b"ac"), b)
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
264 self.assertNotIn(f(b"d"), b)
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
265 self.assertNotIn(f(b"dab"), b)
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
266 self.assertNotIn(f(b"abd"), b)
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
267
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
268 def test_fromhex(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
269 self.assertRaises(TypeError, self.type2test.fromhex)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
270 self.assertRaises(TypeError, self.type2test.fromhex, 1)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60644
diff changeset
271 self.assertEqual(self.type2test.fromhex(''), self.type2test())
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
272 b = bytearray([0x1a, 0x2b, 0x30])
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60644
diff changeset
273 self.assertEqual(self.type2test.fromhex('1a2B30'), b)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60644
diff changeset
274 self.assertEqual(self.type2test.fromhex(' 1A 2B 30 '), b)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60644
diff changeset
275 self.assertEqual(self.type2test.fromhex('0000'), b'\0\0')
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
276 self.assertRaises(TypeError, self.type2test.fromhex, b'1B')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
277 self.assertRaises(ValueError, self.type2test.fromhex, 'a')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
278 self.assertRaises(ValueError, self.type2test.fromhex, 'rt')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
279 self.assertRaises(ValueError, self.type2test.fromhex, '1a b cd')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
280 self.assertRaises(ValueError, self.type2test.fromhex, '\x00')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
281 self.assertRaises(ValueError, self.type2test.fromhex, '12 \x00 34')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
282
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
283 def test_join(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
284 self.assertEqual(self.type2test(b"").join([]), b"")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
285 self.assertEqual(self.type2test(b"").join([b""]), b"")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
286 for lst in [[b"abc"], [b"a", b"bc"], [b"ab", b"c"], [b"a", b"b", b"c"]]:
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
287 lst = list(map(self.type2test, lst))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
288 self.assertEqual(self.type2test(b"").join(lst), b"abc")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
289 self.assertEqual(self.type2test(b"").join(tuple(lst)), b"abc")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
290 self.assertEqual(self.type2test(b"").join(iter(lst)), b"abc")
79766
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
291 dot_join = self.type2test(b".:").join
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
292 self.assertEqual(dot_join([b"ab", b"cd"]), b"ab.:cd")
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
293 self.assertEqual(dot_join([memoryview(b"ab"), b"cd"]), b"ab.:cd")
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
294 self.assertEqual(dot_join([b"ab", memoryview(b"cd")]), b"ab.:cd")
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
295 self.assertEqual(dot_join([bytearray(b"ab"), b"cd"]), b"ab.:cd")
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
296 self.assertEqual(dot_join([b"ab", bytearray(b"cd")]), b"ab.:cd")
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
297 # Stress it with many items
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
298 seq = [b"abc"] * 1000
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
299 expected = b"abc" + b".:abc" * 999
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
300 self.assertEqual(dot_join(seq), expected)
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
301 # Error handling and cleanup when some item in the middle of the
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
302 # sequence has the wrong type.
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
303 with self.assertRaises(TypeError):
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
304 dot_join([bytearray(b"ab"), "cd", b"ef"])
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
305 with self.assertRaises(TypeError):
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
306 dot_join([memoryview(b"ab"), "cd", b"ef"])
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
307
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
308 def test_count(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
309 b = self.type2test(b'mississippi')
73038
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
310 i = 105
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
311 p = 112
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
312 w = 119
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
313
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
314 self.assertEqual(b.count(b'i'), 4)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
315 self.assertEqual(b.count(b'ss'), 2)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
316 self.assertEqual(b.count(b'w'), 0)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
317
73038
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
318 self.assertEqual(b.count(i), 4)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
319 self.assertEqual(b.count(w), 0)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
320
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
321 self.assertEqual(b.count(b'i', 6), 2)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
322 self.assertEqual(b.count(b'p', 6), 2)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
323 self.assertEqual(b.count(b'i', 1, 3), 1)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
324 self.assertEqual(b.count(b'p', 7, 9), 1)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
325
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
326 self.assertEqual(b.count(i, 6), 2)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
327 self.assertEqual(b.count(p, 6), 2)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
328 self.assertEqual(b.count(i, 1, 3), 1)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
329 self.assertEqual(b.count(p, 7, 9), 1)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
330
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
331 def test_startswith(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
332 b = self.type2test(b'hello')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
333 self.assertFalse(self.type2test().startswith(b"anything"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
334 self.assertTrue(b.startswith(b"hello"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
335 self.assertTrue(b.startswith(b"hel"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
336 self.assertTrue(b.startswith(b"h"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
337 self.assertFalse(b.startswith(b"hellow"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
338 self.assertFalse(b.startswith(b"ha"))
69564
f393c507717a #6780: merge with 3.1.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69479 69563
diff changeset
339 with self.assertRaises(TypeError) as cm:
69563
bcbf8c3c4a88 #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69472
diff changeset
340 b.startswith([b'h'])
69564
f393c507717a #6780: merge with 3.1.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69479 69563
diff changeset
341 exc = str(cm.exception)
69563
bcbf8c3c4a88 #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69472
diff changeset
342 self.assertIn('bytes', exc)
bcbf8c3c4a88 #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69472
diff changeset
343 self.assertIn('tuple', exc)
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
344
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
345 def test_endswith(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
346 b = self.type2test(b'hello')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
347 self.assertFalse(bytearray().endswith(b"anything"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
348 self.assertTrue(b.endswith(b"hello"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
349 self.assertTrue(b.endswith(b"llo"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
350 self.assertTrue(b.endswith(b"o"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
351 self.assertFalse(b.endswith(b"whello"))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
352 self.assertFalse(b.endswith(b"no"))
69564
f393c507717a #6780: merge with 3.1.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69479 69563
diff changeset
353 with self.assertRaises(TypeError) as cm:
69563
bcbf8c3c4a88 #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69472
diff changeset
354 b.endswith([b'o'])
69564
f393c507717a #6780: merge with 3.1.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69479 69563
diff changeset
355 exc = str(cm.exception)
69563
bcbf8c3c4a88 #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69472
diff changeset
356 self.assertIn('bytes', exc)
bcbf8c3c4a88 #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69472
diff changeset
357 self.assertIn('tuple', exc)
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
358
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
359 def test_find(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
360 b = self.type2test(b'mississippi')
73038
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
361 i = 105
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
362 w = 119
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
363
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
364 self.assertEqual(b.find(b'ss'), 2)
73038
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
365 self.assertEqual(b.find(b'w'), -1)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
366 self.assertEqual(b.find(b'mississippian'), -1)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
367
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
368 self.assertEqual(b.find(i), 1)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
369 self.assertEqual(b.find(w), -1)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
370
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
371 self.assertEqual(b.find(b'ss', 3), 5)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
372 self.assertEqual(b.find(b'ss', 1, 7), 2)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
373 self.assertEqual(b.find(b'ss', 1, 3), -1)
73038
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
374
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
375 self.assertEqual(b.find(i, 6), 7)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
376 self.assertEqual(b.find(i, 1, 3), 1)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
377 self.assertEqual(b.find(w, 1, 3), -1)
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
378
74022
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73038
diff changeset
379 for index in (-1, 256, sys.maxsize + 1):
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73038
diff changeset
380 self.assertRaisesRegex(
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73038
diff changeset
381 ValueError, r'byte must be in range\(0, 256\)',
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73038
diff changeset
382 b.find, index)
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
383
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
384 def test_rfind(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
385 b = self.type2test(b'mississippi')
73038
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
386 i = 105
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
387 w = 119
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
388
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
389 self.assertEqual(b.rfind(b'ss'), 5)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
390 self.assertEqual(b.rfind(b'w'), -1)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
391 self.assertEqual(b.rfind(b'mississippian'), -1)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
392
73038
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
393 self.assertEqual(b.rfind(i), 10)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
394 self.assertEqual(b.rfind(w), -1)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
395
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
396 self.assertEqual(b.rfind(b'ss', 3), 5)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
397 self.assertEqual(b.rfind(b'ss', 0, 6), 2)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
398
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
399 self.assertEqual(b.rfind(i, 1, 3), 1)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
400 self.assertEqual(b.rfind(i, 3, 9), 7)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
401 self.assertEqual(b.rfind(w, 1, 3), -1)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
402
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
403 def test_index(self):
73038
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
404 b = self.type2test(b'mississippi')
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
405 i = 105
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
406 w = 119
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
407
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
408 self.assertEqual(b.index(b'ss'), 2)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
409 self.assertRaises(ValueError, b.index, b'w')
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
410 self.assertRaises(ValueError, b.index, b'mississippian')
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
411
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
412 self.assertEqual(b.index(i), 1)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
413 self.assertRaises(ValueError, b.index, w)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
414
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
415 self.assertEqual(b.index(b'ss', 3), 5)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
416 self.assertEqual(b.index(b'ss', 1, 7), 2)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
417 self.assertRaises(ValueError, b.index, b'ss', 1, 3)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
418
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
419 self.assertEqual(b.index(i, 6), 7)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
420 self.assertEqual(b.index(i, 1, 3), 1)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
421 self.assertRaises(ValueError, b.index, w, 1, 3)
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
422
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
423 def test_rindex(self):
73038
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
424 b = self.type2test(b'mississippi')
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
425 i = 105
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
426 w = 119
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
427
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
428 self.assertEqual(b.rindex(b'ss'), 5)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
429 self.assertRaises(ValueError, b.rindex, b'w')
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
430 self.assertRaises(ValueError, b.rindex, b'mississippian')
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
431
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
432 self.assertEqual(b.rindex(i), 10)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
433 self.assertRaises(ValueError, b.rindex, w)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
434
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
435 self.assertEqual(b.rindex(b'ss', 3), 5)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
436 self.assertEqual(b.rindex(b'ss', 0, 6), 2)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
437
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
438 self.assertEqual(b.rindex(i, 1, 3), 1)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
439 self.assertEqual(b.rindex(i, 3, 9), 7)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
440 self.assertRaises(ValueError, b.rindex, w, 1, 3)
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
441
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
442 def test_replace(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
443 b = self.type2test(b'mississippi')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
444 self.assertEqual(b.replace(b'i', b'a'), b'massassappa')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
445 self.assertEqual(b.replace(b'ss', b'x'), b'mixixippi')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
446
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
447 def test_split(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
448 b = self.type2test(b'mississippi')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
449 self.assertEqual(b.split(b'i'), [b'm', b'ss', b'ss', b'pp', b''])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
450 self.assertEqual(b.split(b'ss'), [b'mi', b'i', b'ippi'])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
451 self.assertEqual(b.split(b'w'), [b])
75291
db968ac2b82c #14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74022
diff changeset
452 # with keyword args
db968ac2b82c #14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74022
diff changeset
453 b = self.type2test(b'a|b|c|d')
db968ac2b82c #14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74022
diff changeset
454 self.assertEqual(b.split(sep=b'|'), [b'a', b'b', b'c', b'd'])
db968ac2b82c #14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74022
diff changeset
455 self.assertEqual(b.split(b'|', maxsplit=1), [b'a', b'b|c|d'])
db968ac2b82c #14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74022
diff changeset
456 self.assertEqual(b.split(sep=b'|', maxsplit=1), [b'a', b'b|c|d'])
db968ac2b82c #14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74022
diff changeset
457 self.assertEqual(b.split(maxsplit=1, sep=b'|'), [b'a', b'b|c|d'])
db968ac2b82c #14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74022
diff changeset
458 b = self.type2test(b'a b c d')
db968ac2b82c #14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74022
diff changeset
459 self.assertEqual(b.split(maxsplit=1), [b'a', b'b c d'])
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
460
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
461 def test_split_whitespace(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
462 for b in (b' arf barf ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf',
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
463 b'arf\fbarf', b'arf\vbarf'):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
464 b = self.type2test(b)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
465 self.assertEqual(b.split(), [b'arf', b'barf'])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
466 self.assertEqual(b.split(None), [b'arf', b'barf'])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
467 self.assertEqual(b.split(None, 2), [b'arf', b'barf'])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
468 for b in (b'a\x1Cb', b'a\x1Db', b'a\x1Eb', b'a\x1Fb'):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
469 b = self.type2test(b)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
470 self.assertEqual(b.split(), [b])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
471 self.assertEqual(self.type2test(b' a bb c ').split(None, 0), [b'a bb c '])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
472 self.assertEqual(self.type2test(b' a bb c ').split(None, 1), [b'a', b'bb c '])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
473 self.assertEqual(self.type2test(b' a bb c ').split(None, 2), [b'a', b'bb', b'c '])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
474 self.assertEqual(self.type2test(b' a bb c ').split(None, 3), [b'a', b'bb', b'c'])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
475
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
476 def test_split_string_error(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
477 self.assertRaises(TypeError, self.type2test(b'a b').split, ' ')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
478
58276
b7649733a354 Merged revisions 77448 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58264
diff changeset
479 def test_split_unicodewhitespace(self):
b7649733a354 Merged revisions 77448 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58264
diff changeset
480 b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F")
b7649733a354 Merged revisions 77448 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58264
diff changeset
481 self.assertEqual(b.split(), [b'\x1c\x1d\x1e\x1f'])
b7649733a354 Merged revisions 77448 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58264
diff changeset
482
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
483 def test_rsplit(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
484 b = self.type2test(b'mississippi')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
485 self.assertEqual(b.rsplit(b'i'), [b'm', b'ss', b'ss', b'pp', b''])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
486 self.assertEqual(b.rsplit(b'ss'), [b'mi', b'i', b'ippi'])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
487 self.assertEqual(b.rsplit(b'w'), [b])
75291
db968ac2b82c #14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74022
diff changeset
488 # with keyword args
db968ac2b82c #14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74022
diff changeset
489 b = self.type2test(b'a|b|c|d')
db968ac2b82c #14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74022
diff changeset
490 self.assertEqual(b.rsplit(sep=b'|'), [b'a', b'b', b'c', b'd'])
db968ac2b82c #14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74022
diff changeset
491 self.assertEqual(b.rsplit(b'|', maxsplit=1), [b'a|b|c', b'd'])
db968ac2b82c #14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74022
diff changeset
492 self.assertEqual(b.rsplit(sep=b'|', maxsplit=1), [b'a|b|c', b'd'])
db968ac2b82c #14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74022
diff changeset
493 self.assertEqual(b.rsplit(maxsplit=1, sep=b'|'), [b'a|b|c', b'd'])
db968ac2b82c #14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74022
diff changeset
494 b = self.type2test(b'a b c d')
db968ac2b82c #14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74022
diff changeset
495 self.assertEqual(b.rsplit(maxsplit=1), [b'a b c', b'd'])
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
496
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
497 def test_rsplit_whitespace(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
498 for b in (b' arf barf ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf',
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
499 b'arf\fbarf', b'arf\vbarf'):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
500 b = self.type2test(b)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
501 self.assertEqual(b.rsplit(), [b'arf', b'barf'])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
502 self.assertEqual(b.rsplit(None), [b'arf', b'barf'])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
503 self.assertEqual(b.rsplit(None, 2), [b'arf', b'barf'])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
504 self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 0), [b' a bb c'])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
505 self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 1), [b' a bb', b'c'])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
506 self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 2), [b' a', b'bb', b'c'])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
507 self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 3), [b'a', b'bb', b'c'])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
508
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
509 def test_rsplit_string_error(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
510 self.assertRaises(TypeError, self.type2test(b'a b').rsplit, ' ')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
511
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
512 def test_rsplit_unicodewhitespace(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
513 b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
514 self.assertEqual(b.rsplit(), [b'\x1c\x1d\x1e\x1f'])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
515
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
516 def test_partition(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
517 b = self.type2test(b'mississippi')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
518 self.assertEqual(b.partition(b'ss'), (b'mi', b'ss', b'issippi'))
58276
b7649733a354 Merged revisions 77448 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58264
diff changeset
519 self.assertEqual(b.partition(b'w'), (b'mississippi', b'', b''))
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
520
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
521 def test_rpartition(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
522 b = self.type2test(b'mississippi')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
523 self.assertEqual(b.rpartition(b'ss'), (b'missi', b'ss', b'ippi'))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
524 self.assertEqual(b.rpartition(b'i'), (b'mississipp', b'i', b''))
58276
b7649733a354 Merged revisions 77448 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58264
diff changeset
525 self.assertEqual(b.rpartition(b'w'), (b'', b'', b'mississippi'))
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
526
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
527 def test_pickling(self):
49574
bbab11cdb175 Issue #4183: Some tests didn't run with pickle.HIGHEST_PROTOCOL.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49538
diff changeset
528 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
529 for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0":
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
530 b = self.type2test(b)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
531 ps = pickle.dumps(b, proto)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
532 q = pickle.loads(ps)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
533 self.assertEqual(b, q)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
534
76091
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75291
diff changeset
535 def test_iterator_pickling(self):
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75291
diff changeset
536 for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0":
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75291
diff changeset
537 it = itorg = iter(self.type2test(b))
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75291
diff changeset
538 data = list(self.type2test(b))
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75291
diff changeset
539 d = pickle.dumps(it)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75291
diff changeset
540 it = pickle.loads(d)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75291
diff changeset
541 self.assertEqual(type(itorg), type(it))
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75291
diff changeset
542 self.assertEqual(list(it), data)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75291
diff changeset
543
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75291
diff changeset
544 it = pickle.loads(d)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75291
diff changeset
545 try:
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75291
diff changeset
546 next(it)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75291
diff changeset
547 except StopIteration:
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75291
diff changeset
548 continue
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75291
diff changeset
549 d = pickle.dumps(it)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75291
diff changeset
550 it = pickle.loads(d)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75291
diff changeset
551 self.assertEqual(list(it), data[1:])
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75291
diff changeset
552
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
553 def test_strip(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
554 b = self.type2test(b'mississippi')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
555 self.assertEqual(b.strip(b'i'), b'mississipp')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
556 self.assertEqual(b.strip(b'm'), b'ississippi')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
557 self.assertEqual(b.strip(b'pi'), b'mississ')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
558 self.assertEqual(b.strip(b'im'), b'ssissipp')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
559 self.assertEqual(b.strip(b'pim'), b'ssiss')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
560 self.assertEqual(b.strip(b), b'')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
561
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
562 def test_lstrip(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
563 b = self.type2test(b'mississippi')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
564 self.assertEqual(b.lstrip(b'i'), b'mississippi')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
565 self.assertEqual(b.lstrip(b'm'), b'ississippi')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
566 self.assertEqual(b.lstrip(b'pi'), b'mississippi')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
567 self.assertEqual(b.lstrip(b'im'), b'ssissippi')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
568 self.assertEqual(b.lstrip(b'pim'), b'ssissippi')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
569
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
570 def test_rstrip(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
571 b = self.type2test(b'mississippi')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
572 self.assertEqual(b.rstrip(b'i'), b'mississipp')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
573 self.assertEqual(b.rstrip(b'm'), b'mississippi')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
574 self.assertEqual(b.rstrip(b'pi'), b'mississ')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
575 self.assertEqual(b.rstrip(b'im'), b'mississipp')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
576 self.assertEqual(b.rstrip(b'pim'), b'mississ')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
577
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
578 def test_strip_whitespace(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
579 b = self.type2test(b' \t\n\r\f\vabc \t\n\r\f\v')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
580 self.assertEqual(b.strip(), b'abc')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
581 self.assertEqual(b.lstrip(), b'abc \t\n\r\f\v')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
582 self.assertEqual(b.rstrip(), b' \t\n\r\f\vabc')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
583
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
584 def test_strip_bytearray(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
585 self.assertEqual(self.type2test(b'abc').strip(memoryview(b'ac')), b'b')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
586 self.assertEqual(self.type2test(b'abc').lstrip(memoryview(b'ac')), b'bc')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
587 self.assertEqual(self.type2test(b'abc').rstrip(memoryview(b'ac')), b'ab')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
588
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
589 def test_strip_string_error(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
590 self.assertRaises(TypeError, self.type2test(b'abc').strip, 'b')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
591 self.assertRaises(TypeError, self.type2test(b'abc').lstrip, 'b')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
592 self.assertRaises(TypeError, self.type2test(b'abc').rstrip, 'b')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
593
71543
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
594 def test_center(self):
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
595 # Fill character can be either bytes or bytearray (issue 12380)
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
596 b = self.type2test(b'abc')
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
597 for fill_type in (bytes, bytearray):
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
598 self.assertEqual(b.center(7, fill_type(b'-')),
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
599 self.type2test(b'--abc--'))
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
600
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
601 def test_ljust(self):
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
602 # Fill character can be either bytes or bytearray (issue 12380)
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
603 b = self.type2test(b'abc')
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
604 for fill_type in (bytes, bytearray):
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
605 self.assertEqual(b.ljust(7, fill_type(b'-')),
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
606 self.type2test(b'abc----'))
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
607
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
608 def test_rjust(self):
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
609 # Fill character can be either bytes or bytearray (issue 12380)
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
610 b = self.type2test(b'abc')
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
611 for fill_type in (bytes, bytearray):
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
612 self.assertEqual(b.rjust(7, fill_type(b'-')),
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
613 self.type2test(b'----abc'))
536fccc75f5a Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
Eli Bendersky <eliben@gmail.com>
parents: 69565
diff changeset
614
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
615 def test_ord(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
616 b = self.type2test(b'\0A\x7f\x80\xff')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
617 self.assertEqual([ord(b[i:i+1]) for i in range(len(b))],
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
618 [0, 65, 127, 128, 255])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
619
53094
4407e4d07084 Add bytes/bytearray.maketrans() to mirror str.maketrans(), and deprecate
Georg Brandl <georg@python.org>
parents: 50328
diff changeset
620 def test_maketrans(self):
4407e4d07084 Add bytes/bytearray.maketrans() to mirror str.maketrans(), and deprecate
Georg Brandl <georg@python.org>
parents: 50328
diff changeset
621 transtable = b'\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`xyzdefghijklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377'
4407e4d07084 Add bytes/bytearray.maketrans() to mirror str.maketrans(), and deprecate
Georg Brandl <georg@python.org>
parents: 50328
diff changeset
622 self.assertEqual(self.type2test.maketrans(b'abc', b'xyz'), transtable)
56515
67bb17c2e868 Merged revisions 75404 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55848
diff changeset
623 transtable = b'\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374xyz'
67bb17c2e868 Merged revisions 75404 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55848
diff changeset
624 self.assertEqual(self.type2test.maketrans(b'\375\376\377', b'xyz'), transtable)
53094
4407e4d07084 Add bytes/bytearray.maketrans() to mirror str.maketrans(), and deprecate
Georg Brandl <georg@python.org>
parents: 50328
diff changeset
625 self.assertRaises(ValueError, self.type2test.maketrans, b'abc', b'xyzq')
4407e4d07084 Add bytes/bytearray.maketrans() to mirror str.maketrans(), and deprecate
Georg Brandl <georg@python.org>
parents: 50328
diff changeset
626 self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 'def')
4407e4d07084 Add bytes/bytearray.maketrans() to mirror str.maketrans(), and deprecate
Georg Brandl <georg@python.org>
parents: 50328
diff changeset
627
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
628 def test_none_arguments(self):
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
629 # issue 11828
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
630 b = self.type2test(b'hello')
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
631 l = self.type2test(b'l')
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
632 h = self.type2test(b'h')
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
633 x = self.type2test(b'x')
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
634 o = self.type2test(b'o')
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
635
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
636 self.assertEqual(2, b.find(l, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
637 self.assertEqual(3, b.find(l, -2, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
638 self.assertEqual(2, b.find(l, None, -2))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
639 self.assertEqual(0, b.find(h, None, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
640
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
641 self.assertEqual(3, b.rfind(l, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
642 self.assertEqual(3, b.rfind(l, -2, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
643 self.assertEqual(2, b.rfind(l, None, -2))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
644 self.assertEqual(0, b.rfind(h, None, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
645
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
646 self.assertEqual(2, b.index(l, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
647 self.assertEqual(3, b.index(l, -2, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
648 self.assertEqual(2, b.index(l, None, -2))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
649 self.assertEqual(0, b.index(h, None, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
650
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
651 self.assertEqual(3, b.rindex(l, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
652 self.assertEqual(3, b.rindex(l, -2, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
653 self.assertEqual(2, b.rindex(l, None, -2))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
654 self.assertEqual(0, b.rindex(h, None, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
655
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
656 self.assertEqual(2, b.count(l, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
657 self.assertEqual(1, b.count(l, -2, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
658 self.assertEqual(1, b.count(l, None, -2))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
659 self.assertEqual(0, b.count(x, None, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
660
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
661 self.assertEqual(True, b.endswith(o, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
662 self.assertEqual(True, b.endswith(o, -2, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
663 self.assertEqual(True, b.endswith(l, None, -2))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
664 self.assertEqual(False, b.endswith(x, None, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
665
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
666 self.assertEqual(True, b.startswith(h, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
667 self.assertEqual(True, b.startswith(l, -2, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
668 self.assertEqual(True, b.startswith(h, None, -2))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
669 self.assertEqual(False, b.startswith(x, None, None))
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
670
73038
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
671 def test_integer_arguments_out_of_byte_range(self):
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
672 b = self.type2test(b'hello')
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
673
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
674 for method in (b.count, b.find, b.index, b.rfind, b.rindex):
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
675 self.assertRaises(ValueError, method, -1)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
676 self.assertRaises(ValueError, method, 256)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
677 self.assertRaises(ValueError, method, 9999)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
678
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
679 def test_find_etc_raise_correct_error_messages(self):
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
680 # issue 11828
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
681 b = self.type2test(b'hello')
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
682 x = self.type2test(b'x')
69479
87f9519203f4 Use non-deprecated method name.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69473
diff changeset
683 self.assertRaisesRegex(TypeError, r'\bfind\b', b.find,
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
684 x, None, None, None)
69479
87f9519203f4 Use non-deprecated method name.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69473
diff changeset
685 self.assertRaisesRegex(TypeError, r'\brfind\b', b.rfind,
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
686 x, None, None, None)
69479
87f9519203f4 Use non-deprecated method name.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69473
diff changeset
687 self.assertRaisesRegex(TypeError, r'\bindex\b', b.index,
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
688 x, None, None, None)
69479
87f9519203f4 Use non-deprecated method name.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69473
diff changeset
689 self.assertRaisesRegex(TypeError, r'\brindex\b', b.rindex,
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
690 x, None, None, None)
69479
87f9519203f4 Use non-deprecated method name.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69473
diff changeset
691 self.assertRaisesRegex(TypeError, r'\bcount\b', b.count,
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
692 x, None, None, None)
69479
87f9519203f4 Use non-deprecated method name.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69473
diff changeset
693 self.assertRaisesRegex(TypeError, r'\bstartswith\b', b.startswith,
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
694 x, None, None, None)
69479
87f9519203f4 Use non-deprecated method name.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69473
diff changeset
695 self.assertRaisesRegex(TypeError, r'\bendswith\b', b.endswith,
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
696 x, None, None, None)
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 66340
diff changeset
697
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
698
81356
55383912c225 #16910: test_bytes, test_unicode, and test_userstring now work with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80954
diff changeset
699 class BytesTest(BaseBytesTest, unittest.TestCase):
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
700 type2test = bytes
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
701
48623
bf50085fbcd3 #2538: bytes objects can only provide read-only buffers
Antoine Pitrou <solipsis@pitrou.net>
parents: 48508
diff changeset
702 def test_buffer_is_readonly(self):
48710
e0c65922b878 Issue #3571: test_bytes mistakingly closed stdin
Antoine Pitrou <solipsis@pitrou.net>
parents: 48623
diff changeset
703 fd = os.dup(sys.stdin.fileno())
e0c65922b878 Issue #3571: test_bytes mistakingly closed stdin
Antoine Pitrou <solipsis@pitrou.net>
parents: 48623
diff changeset
704 with open(fd, "rb", buffering=0) as f:
48623
bf50085fbcd3 #2538: bytes objects can only provide read-only buffers
Antoine Pitrou <solipsis@pitrou.net>
parents: 48508
diff changeset
705 self.assertRaises(TypeError, f.readinto, b"")
bf50085fbcd3 #2538: bytes objects can only provide read-only buffers
Antoine Pitrou <solipsis@pitrou.net>
parents: 48508
diff changeset
706
48871
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
707 def test_custom(self):
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
708 class A:
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
709 def __bytes__(self):
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
710 return b'abc'
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
711 self.assertEqual(bytes(A()), b'abc')
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
712 class A: pass
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
713 self.assertRaises(TypeError, bytes, A())
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
714 class A:
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
715 def __bytes__(self):
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
716 return None
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
717 self.assertRaises(TypeError, bytes, A())
80954
c744b6f8a09a try to call __bytes__ before __index__ (closes #16722)
Benjamin Peterson <benjamin@python.org>
parents: 80210
diff changeset
718 class A:
c744b6f8a09a try to call __bytes__ before __index__ (closes #16722)
Benjamin Peterson <benjamin@python.org>
parents: 80210
diff changeset
719 def __bytes__(self):
c744b6f8a09a try to call __bytes__ before __index__ (closes #16722)
Benjamin Peterson <benjamin@python.org>
parents: 80210
diff changeset
720 return b'a'
c744b6f8a09a try to call __bytes__ before __index__ (closes #16722)
Benjamin Peterson <benjamin@python.org>
parents: 80210
diff changeset
721 def __index__(self):
c744b6f8a09a try to call __bytes__ before __index__ (closes #16722)
Benjamin Peterson <benjamin@python.org>
parents: 80210
diff changeset
722 return 42
c744b6f8a09a try to call __bytes__ before __index__ (closes #16722)
Benjamin Peterson <benjamin@python.org>
parents: 80210
diff changeset
723 self.assertEqual(bytes(A()), b'a')
48871
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
724
67368
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
725 # Test PyBytes_FromFormat()
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
726 def test_from_format(self):
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
727 test.support.import_module('ctypes')
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
728 from ctypes import pythonapi, py_object, c_int, c_char_p
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
729 PyBytes_FromFormat = pythonapi.PyBytes_FromFormat
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
730 PyBytes_FromFormat.restype = py_object
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
731
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
732 self.assertEqual(PyBytes_FromFormat(b'format'),
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
733 b'format')
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
734
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
735 self.assertEqual(PyBytes_FromFormat(b'%'), b'%')
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
736 self.assertEqual(PyBytes_FromFormat(b'%%'), b'%')
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
737 self.assertEqual(PyBytes_FromFormat(b'%%s'), b'%s')
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
738 self.assertEqual(PyBytes_FromFormat(b'[%%]'), b'[%]')
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
739 self.assertEqual(PyBytes_FromFormat(b'%%%c', c_int(ord('_'))), b'%_')
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
740
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
741 self.assertEqual(PyBytes_FromFormat(b'c:%c', c_int(255)),
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
742 b'c:\xff')
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
743 self.assertEqual(PyBytes_FromFormat(b's:%s', c_char_p(b'cstr')),
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
744 b's:cstr')
4cc47a9083e3 test_bytes: test PyBytes_FromFormat() using ctypes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66837
diff changeset
745
48623
bf50085fbcd3 #2538: bytes objects can only provide read-only buffers
Antoine Pitrou <solipsis@pitrou.net>
parents: 48508
diff changeset
746
81356
55383912c225 #16910: test_bytes, test_unicode, and test_userstring now work with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80954
diff changeset
747 class ByteArrayTest(BaseBytesTest, unittest.TestCase):
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
748 type2test = bytearray
41412
522b3f35d0eb Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents: 41411
diff changeset
749
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
750 def test_nohash(self):
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
751 self.assertRaises(TypeError, hash, bytearray())
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
752
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
753 def test_bytearray_api(self):
41855
057a9c3d067d Fix tset_bytes.py.
Guido van Rossum <guido@python.org>
parents: 41664
diff changeset
754 short_sample = b"Hello world\n"
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
755 sample = short_sample + b"\0"*(20 - len(short_sample))
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
756 tfn = tempfile.mktemp()
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
757 try:
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
758 # Prepare
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
759 with open(tfn, "wb") as f:
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
760 f.write(short_sample)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
761 # Test readinto
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
762 with open(tfn, "rb") as f:
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
763 b = bytearray(20)
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
764 n = f.readinto(b)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
765 self.assertEqual(n, len(short_sample))
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
766 self.assertEqual(list(b), list(sample))
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
767 # Test writing in binary mode
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
768 with open(tfn, "wb") as f:
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
769 f.write(b)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
770 with open(tfn, "rb") as f:
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
771 self.assertEqual(f.read(), sample)
41455
5dfd2f4bc834 Support name and mode attributes on all file types.
Guido van Rossum <guido@python.org>
parents: 41451
diff changeset
772 # Text mode is ambiguous; don't test
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
773 finally:
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
774 try:
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
775 os.remove(tfn)
81017
6cfe2982de42 Use OESeeror instead of os.error (#16720)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80955
diff changeset
776 except OSError:
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
777 pass
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
778
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
779 def test_reverse(self):
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
780 b = bytearray(b'hello')
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
781 self.assertEqual(b.reverse(), None)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
782 self.assertEqual(b, b'olleh')
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
783 b = bytearray(b'hello1') # test even number of items
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
784 b.reverse()
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
785 self.assertEqual(b, b'1olleh')
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
786 b = bytearray()
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
787 b.reverse()
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
788 self.assertFalse(b)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
789
68221
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
790 def test_clear(self):
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
791 b = bytearray(b'python')
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
792 b.clear()
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
793 self.assertEqual(b, b'')
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
794
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
795 b = bytearray(b'')
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
796 b.clear()
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
797 self.assertEqual(b, b'')
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
798
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
799 b = bytearray(b'')
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
800 b.append(ord('r'))
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
801 b.clear()
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
802 b.append(ord('p'))
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
803 self.assertEqual(b, b'p')
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
804
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
805 def test_copy(self):
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
806 b = bytearray(b'abc')
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
807 bb = b.copy()
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
808 self.assertEqual(bb, b'abc')
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
809
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
810 b = bytearray(b'')
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
811 bb = b.copy()
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
812 self.assertEqual(bb, b'')
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
813
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
814 # test that it's indeed a copy and not a reference
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
815 b = bytearray(b'abc')
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
816 bb = b.copy()
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
817 self.assertEqual(b, bb)
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
818 self.assertIsNot(b, bb)
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
819 bb.append(ord('d'))
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
820 self.assertEqual(bb, b'abcd')
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
821 self.assertEqual(b, b'abc')
08804aea08f1 Issue #10516: added copy() and clear() methods to bytearrays as well
Eli Bendersky <eliben@gmail.com>
parents: 68142
diff changeset
822
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
823 def test_regexps(self):
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
824 def by(s):
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
825 return bytearray(map(ord, s))
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
826 b = by("Hello, world")
48769
83b2d07b475b #2834: Change re module semantics, so that str and bytes mixing is forbidden,
Antoine Pitrou <solipsis@pitrou.net>
parents: 48710
diff changeset
827 self.assertEqual(re.findall(br"\w+", b), [by("Hello"), by("world")])
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
828
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
829 def test_setitem(self):
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
830 b = bytearray([1, 2, 3])
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
831 b[1] = 100
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
832 self.assertEqual(b, bytearray([1, 100, 3]))
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
833 b[-1] = 200
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
834 self.assertEqual(b, bytearray([1, 100, 200]))
48508
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
835 b[0] = Indexable(10)
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
836 self.assertEqual(b, bytearray([10, 100, 200]))
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
837 try:
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
838 b[3] = 0
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
839 self.fail("Didn't raise IndexError")
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
840 except IndexError:
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
841 pass
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
842 try:
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
843 b[-10] = 0
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
844 self.fail("Didn't raise IndexError")
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
845 except IndexError:
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
846 pass
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
847 try:
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
848 b[0] = 256
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
849 self.fail("Didn't raise ValueError")
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
850 except ValueError:
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
851 pass
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
852 try:
48508
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
853 b[0] = Indexable(-1)
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
854 self.fail("Didn't raise ValueError")
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
855 except ValueError:
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
856 pass
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
857 try:
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
858 b[0] = None
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
859 self.fail("Didn't raise TypeError")
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
860 except TypeError:
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
861 pass
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
862
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
863 def test_delitem(self):
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
864 b = bytearray(range(10))
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
865 del b[0]
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
866 self.assertEqual(b, bytearray(range(1, 10)))
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
867 del b[-1]
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
868 self.assertEqual(b, bytearray(range(1, 9)))
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
869 del b[4]
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
870 self.assertEqual(b, bytearray([1, 2, 3, 4, 6, 7, 8]))
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
871
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
872 def test_setslice(self):
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
873 b = bytearray(range(10))
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
874 self.assertEqual(list(b), list(range(10)))
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
875
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
876 b[0:5] = bytearray([1, 1, 1, 1, 1])
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
877 self.assertEqual(b, bytearray([1, 1, 1, 1, 1, 5, 6, 7, 8, 9]))
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
878
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
879 del b[0:-5]
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
880 self.assertEqual(b, bytearray([5, 6, 7, 8, 9]))
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
881
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
882 b[0:0] = bytearray([0, 1, 2, 3, 4])
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
883 self.assertEqual(b, bytearray(range(10)))
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
884
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
885 b[-7:-3] = bytearray([100, 101])
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
886 self.assertEqual(b, bytearray([0, 1, 2, 100, 101, 7, 8, 9]))
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
887
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
888 b[3:5] = [3, 4, 5, 6]
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
889 self.assertEqual(b, bytearray(range(10)))
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41425
diff changeset
890
39643
3dd61ffd049e Fix buglet in slice assignment of bytesobjects: assigning to b[3:0] ('stop'
Thomas Wouters <thomas@python.org>
parents: 39631
diff changeset
891 b[3:0] = [42, 42, 42]
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
892 self.assertEqual(b, bytearray([0, 1, 2, 42, 42, 42, 3, 4, 5, 6, 7, 8, 9]))
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
893
80209
8f00af8abaf9 #8401: assigning an int to a bytearray slice (e.g. b[3:4] = 5) now raises an error.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69564
diff changeset
894 b[3:] = b'foo'
8f00af8abaf9 #8401: assigning an int to a bytearray slice (e.g. b[3:4] = 5) now raises an error.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69564
diff changeset
895 self.assertEqual(b, bytearray([0, 1, 2, 102, 111, 111]))
8f00af8abaf9 #8401: assigning an int to a bytearray slice (e.g. b[3:4] = 5) now raises an error.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69564
diff changeset
896
8f00af8abaf9 #8401: assigning an int to a bytearray slice (e.g. b[3:4] = 5) now raises an error.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69564
diff changeset
897 b[:3] = memoryview(b'foo')
8f00af8abaf9 #8401: assigning an int to a bytearray slice (e.g. b[3:4] = 5) now raises an error.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69564
diff changeset
898 self.assertEqual(b, bytearray([102, 111, 111, 102, 111, 111]))
8f00af8abaf9 #8401: assigning an int to a bytearray slice (e.g. b[3:4] = 5) now raises an error.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69564
diff changeset
899
8f00af8abaf9 #8401: assigning an int to a bytearray slice (e.g. b[3:4] = 5) now raises an error.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69564
diff changeset
900 b[3:4] = []
8f00af8abaf9 #8401: assigning an int to a bytearray slice (e.g. b[3:4] = 5) now raises an error.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69564
diff changeset
901 self.assertEqual(b, bytearray([102, 111, 111, 111, 111]))
8f00af8abaf9 #8401: assigning an int to a bytearray slice (e.g. b[3:4] = 5) now raises an error.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69564
diff changeset
902
8f00af8abaf9 #8401: assigning an int to a bytearray slice (e.g. b[3:4] = 5) now raises an error.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69564
diff changeset
903 for elem in [5, -5, 0, int(10e20), 'str', 2.3,
8f00af8abaf9 #8401: assigning an int to a bytearray slice (e.g. b[3:4] = 5) now raises an error.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69564
diff changeset
904 ['a', 'b'], [b'a', b'b'], [[]]]:
8f00af8abaf9 #8401: assigning an int to a bytearray slice (e.g. b[3:4] = 5) now raises an error.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69564
diff changeset
905 with self.assertRaises(TypeError):
8f00af8abaf9 #8401: assigning an int to a bytearray slice (e.g. b[3:4] = 5) now raises an error.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69564
diff changeset
906 b[3:4] = elem
8f00af8abaf9 #8401: assigning an int to a bytearray slice (e.g. b[3:4] = 5) now raises an error.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69564
diff changeset
907
8f00af8abaf9 #8401: assigning an int to a bytearray slice (e.g. b[3:4] = 5) now raises an error.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69564
diff changeset
908 for elem in [[254, 255, 256], [-256, 9000]]:
8f00af8abaf9 #8401: assigning an int to a bytearray slice (e.g. b[3:4] = 5) now raises an error.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69564
diff changeset
909 with self.assertRaises(ValueError):
8f00af8abaf9 #8401: assigning an int to a bytearray slice (e.g. b[3:4] = 5) now raises an error.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69564
diff changeset
910 b[3:4] = elem
8f00af8abaf9 #8401: assigning an int to a bytearray slice (e.g. b[3:4] = 5) now raises an error.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69564
diff changeset
911
85969
499a96611baa Issue #19087: Improve bytearray allocation in order to allow cheap popping of data at the front (slice deletion).
Antoine Pitrou <solipsis@pitrou.net>
parents: 81783
diff changeset
912 def test_setslice_extend(self):
499a96611baa Issue #19087: Improve bytearray allocation in order to allow cheap popping of data at the front (slice deletion).
Antoine Pitrou <solipsis@pitrou.net>
parents: 81783
diff changeset
913 # Exercise the resizing logic (see issue #19087)
499a96611baa Issue #19087: Improve bytearray allocation in order to allow cheap popping of data at the front (slice deletion).
Antoine Pitrou <solipsis@pitrou.net>
parents: 81783
diff changeset
914 b = bytearray(range(100))
499a96611baa Issue #19087: Improve bytearray allocation in order to allow cheap popping of data at the front (slice deletion).
Antoine Pitrou <solipsis@pitrou.net>
parents: 81783
diff changeset
915 self.assertEqual(list(b), list(range(100)))
499a96611baa Issue #19087: Improve bytearray allocation in order to allow cheap popping of data at the front (slice deletion).
Antoine Pitrou <solipsis@pitrou.net>
parents: 81783
diff changeset
916 del b[:10]
499a96611baa Issue #19087: Improve bytearray allocation in order to allow cheap popping of data at the front (slice deletion).
Antoine Pitrou <solipsis@pitrou.net>
parents: 81783
diff changeset
917 self.assertEqual(list(b), list(range(10, 100)))
499a96611baa Issue #19087: Improve bytearray allocation in order to allow cheap popping of data at the front (slice deletion).
Antoine Pitrou <solipsis@pitrou.net>
parents: 81783
diff changeset
918 b.extend(range(100, 110))
499a96611baa Issue #19087: Improve bytearray allocation in order to allow cheap popping of data at the front (slice deletion).
Antoine Pitrou <solipsis@pitrou.net>
parents: 81783
diff changeset
919 self.assertEqual(list(b), list(range(10, 110)))
499a96611baa Issue #19087: Improve bytearray allocation in order to allow cheap popping of data at the front (slice deletion).
Antoine Pitrou <solipsis@pitrou.net>
parents: 81783
diff changeset
920
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
921 def test_extended_set_del_slice(self):
58564
6de64896c6f8 Merged revisions 77823 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 58389
diff changeset
922 indices = (0, None, 1, 3, 19, 300, 1<<333, -1, -2, -31, -300)
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
923 for start in indices:
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
924 for stop in indices:
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
925 # Skip invalid step 0
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
926 for step in indices[1:]:
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
927 L = list(range(255))
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
928 b = bytearray(L)
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
929 # Make sure we have a slice of exactly the right length,
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
930 # but with different data.
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
931 data = L[start:stop:step]
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
932 data.reverse()
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
933 L[start:stop:step] = data
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
934 b[start:stop:step] = data
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60644
diff changeset
935 self.assertEqual(b, bytearray(L))
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41425
diff changeset
936
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
937 del L[start:stop:step]
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
938 del b[start:stop:step]
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60644
diff changeset
939 self.assertEqual(b, bytearray(L))
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
940
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
941 def test_setslice_trap(self):
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
942 # This test verifies that we correctly handle assigning self
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
943 # to a slice of self (the old Lambert Meertens trap).
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
944 b = bytearray(range(256))
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
945 b[8:] = b
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
946 self.assertEqual(b, bytearray(list(range(8)) + list(range(256))))
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
947
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
948 def test_iconcat(self):
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
949 b = bytearray(b"abc")
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
950 b1 = b
42907
0170418dadea Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42499
diff changeset
951 b += b"def"
0170418dadea Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42499
diff changeset
952 self.assertEqual(b, b"abcdef")
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
953 self.assertEqual(b, b1)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
954 self.assertTrue(b is b1)
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
955 b += b"xyz"
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41425
diff changeset
956 self.assertEqual(b, b"abcdefxyz")
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41425
diff changeset
957 try:
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41455
diff changeset
958 b += ""
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41425
diff changeset
959 except TypeError:
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41425
diff changeset
960 pass
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41425
diff changeset
961 else:
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41425
diff changeset
962 self.fail("bytes += unicode didn't raise TypeError")
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
963
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
964 def test_irepeat(self):
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
965 b = bytearray(b"abc")
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
966 b1 = b
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
967 b *= 3
42907
0170418dadea Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42499
diff changeset
968 self.assertEqual(b, b"abcabcabc")
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
969 self.assertEqual(b, b1)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
970 self.assertTrue(b is b1)
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
971
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
972 def test_irepeat_1char(self):
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
973 b = bytearray(b"x")
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
974 b1 = b
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
975 b *= 100
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
976 self.assertEqual(b, b"x"*100)
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
977 self.assertEqual(b, b1)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
978 self.assertTrue(b is b1)
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
979
38117
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
980 def test_alloc(self):
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
981 b = bytearray()
38117
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
982 alloc = b.__alloc__()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
983 self.assertTrue(alloc >= 0)
38117
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
984 seq = [alloc]
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
985 for i in range(100):
42907
0170418dadea Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42499
diff changeset
986 b += b"x"
38117
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
987 alloc = b.__alloc__()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
988 self.assertTrue(alloc >= len(b))
38117
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
989 if alloc not in seq:
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
990 seq.append(alloc)
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
991
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
992 def test_extend(self):
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
993 orig = b'hello'
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
994 a = bytearray(orig)
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
995 a.extend(a)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
996 self.assertEqual(a, orig + orig)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
997 self.assertEqual(a[5:], orig)
44137
825cffd6c0cd Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43963
diff changeset
998 a = bytearray(b'')
825cffd6c0cd Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43963
diff changeset
999 # Test iterators that don't have a __length_hint__
825cffd6c0cd Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43963
diff changeset
1000 a.extend(map(int, orig * 25))
825cffd6c0cd Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43963
diff changeset
1001 a.extend(int(x) for x in orig * 25)
825cffd6c0cd Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43963
diff changeset
1002 self.assertEqual(a, orig * 50)
825cffd6c0cd Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43963
diff changeset
1003 self.assertEqual(a[-5:], orig)
825cffd6c0cd Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43963
diff changeset
1004 a = bytearray(b'')
825cffd6c0cd Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43963
diff changeset
1005 a.extend(iter(map(int, orig * 50)))
825cffd6c0cd Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43963
diff changeset
1006 self.assertEqual(a, orig * 50)
825cffd6c0cd Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43963
diff changeset
1007 self.assertEqual(a[-5:], orig)
825cffd6c0cd Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43963
diff changeset
1008 a = bytearray(b'')
825cffd6c0cd Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43963
diff changeset
1009 a.extend(list(map(int, orig * 50)))
825cffd6c0cd Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43963
diff changeset
1010 self.assertEqual(a, orig * 50)
825cffd6c0cd Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43963
diff changeset
1011 self.assertEqual(a[-5:], orig)
825cffd6c0cd Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43963
diff changeset
1012 a = bytearray(b'')
825cffd6c0cd Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43963
diff changeset
1013 self.assertRaises(ValueError, a.extend, [0, 1, 2, 256])
825cffd6c0cd Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43963
diff changeset
1014 self.assertRaises(ValueError, a.extend, [0, 1, 2, -1])
825cffd6c0cd Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43963
diff changeset
1015 self.assertEqual(len(a), 0)
48508
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
1016 a = bytearray(b'')
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
1017 a.extend([Indexable(ord('a'))])
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
1018 self.assertEqual(a, b'a')
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41425
diff changeset
1019
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1020 def test_remove(self):
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
1021 b = bytearray(b'hello')
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1022 b.remove(ord('l'))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1023 self.assertEqual(b, b'helo')
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1024 b.remove(ord('l'))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1025 self.assertEqual(b, b'heo')
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1026 self.assertRaises(ValueError, lambda: b.remove(ord('l')))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1027 self.assertRaises(ValueError, lambda: b.remove(400))
43576
697cc31a83f6 For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents: 43508
diff changeset
1028 self.assertRaises(TypeError, lambda: b.remove('e'))
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1029 # remove first and last
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1030 b.remove(ord('o'))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1031 b.remove(ord('h'))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1032 self.assertEqual(b, b'e')
43576
697cc31a83f6 For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents: 43508
diff changeset
1033 self.assertRaises(TypeError, lambda: b.remove(b'e'))
48508
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
1034 b.remove(Indexable(ord('e')))
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
1035 self.assertEqual(b, b'')
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1036
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1037 def test_pop(self):
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
1038 b = bytearray(b'world')
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1039 self.assertEqual(b.pop(), ord('d'))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1040 self.assertEqual(b.pop(0), ord('w'))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1041 self.assertEqual(b.pop(-2), ord('r'))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1042 self.assertRaises(IndexError, lambda: b.pop(10))
68067
1a05bc36a334 Merged revisions 88735 via svnmerge from
Eli Bendersky <eliben@gmail.com>
parents: 67368
diff changeset
1043 self.assertRaises(IndexError, lambda: bytearray().pop())
55848
1f4f342bfe2e Merged revisions 74675 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55593
diff changeset
1044 # test for issue #6846
1f4f342bfe2e Merged revisions 74675 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55593
diff changeset
1045 self.assertEqual(bytearray(b'\xff').pop(), 0xff)
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1046
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1047 def test_nosort(self):
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
1048 self.assertRaises(AttributeError, lambda: bytearray().sort())
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1049
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1050 def test_append(self):
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
1051 b = bytearray(b'hell')
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1052 b.append(ord('o'))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1053 self.assertEqual(b, b'hello')
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1054 self.assertEqual(b.append(100), None)
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
1055 b = bytearray()
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1056 b.append(ord('A'))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1057 self.assertEqual(len(b), 1)
43576
697cc31a83f6 For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents: 43508
diff changeset
1058 self.assertRaises(TypeError, lambda: b.append(b'o'))
48508
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
1059 b = bytearray()
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
1060 b.append(Indexable(ord('A')))
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
1061 self.assertEqual(b, b'A')
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1062
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1063 def test_insert(self):
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
1064 b = bytearray(b'msssspp')
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1065 b.insert(1, ord('i'))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1066 b.insert(4, ord('i'))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1067 b.insert(-2, ord('i'))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1068 b.insert(1000, ord('i'))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1069 self.assertEqual(b, b'mississippi')
43576
697cc31a83f6 For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents: 43508
diff changeset
1070 self.assertRaises(TypeError, lambda: b.insert(0, b'1'))
48508
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
1071 b = bytearray()
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
1072 b.insert(0, Indexable(ord('A')))
b9828b2a51c0 Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47391
diff changeset
1073 self.assertEqual(b, b'A')
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1074
49770
fdc933951964 Merged revisions 67291 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49574
diff changeset
1075 def test_copied(self):
fdc933951964 Merged revisions 67291 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49574
diff changeset
1076 # Issue 4348. Make sure that operations that don't mutate the array
fdc933951964 Merged revisions 67291 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49574
diff changeset
1077 # copy the bytes.
fdc933951964 Merged revisions 67291 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49574
diff changeset
1078 b = bytearray(b'abc')
49819
630c174b465e Merged revisions 67295,67301-67302,67318,67330,67342-67343 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49770
diff changeset
1079 self.assertFalse(b is b.replace(b'abc', b'cde', 0))
49770
fdc933951964 Merged revisions 67291 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49574
diff changeset
1080
fdc933951964 Merged revisions 67291 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49574
diff changeset
1081 t = bytearray([i for i in range(256)])
fdc933951964 Merged revisions 67291 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49574
diff changeset
1082 x = bytearray(b'')
fdc933951964 Merged revisions 67291 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49574
diff changeset
1083 self.assertFalse(x is x.translate(t))
fdc933951964 Merged revisions 67291 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49574
diff changeset
1084
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
1085 def test_partition_bytearray_doesnt_share_nullstring(self):
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
1086 a, b, c = bytearray(b"x").partition(b"y")
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
1087 self.assertEqual(b, b"")
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
1088 self.assertEqual(c, b"")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
1089 self.assertTrue(b is not c)
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
1090 b += b"!"
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
1091 self.assertEqual(c, b"")
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
1092 a, b, c = bytearray(b"x").partition(b"y")
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
1093 self.assertEqual(b, b"")
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
1094 self.assertEqual(c, b"")
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
1095 # Same for rpartition
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
1096 b, c, a = bytearray(b"x").rpartition(b"y")
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
1097 self.assertEqual(b, b"")
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
1098 self.assertEqual(c, b"")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
1099 self.assertTrue(b is not c)
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
1100 b += b"!"
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
1101 self.assertEqual(c, b"")
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
1102 c, b, a = bytearray(b"x").rpartition(b"y")
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
1103 self.assertEqual(b, b"")
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
1104 self.assertEqual(c, b"")
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
1105
50056
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1106 def test_resize_forbidden(self):
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1107 # #4509: can't resize a bytearray when there are buffer exports, even
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1108 # if it wouldn't reallocate the underlying buffer.
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1109 # Furthermore, no destructive changes to the buffer may be applied
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1110 # before raising the error.
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1111 b = bytearray(range(10))
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1112 v = memoryview(b)
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1113 def resize(n):
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1114 b[1:-1] = range(n + 1, 2*n - 1)
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1115 resize(10)
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1116 orig = b[:]
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1117 self.assertRaises(BufferError, resize, 11)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60644
diff changeset
1118 self.assertEqual(b, orig)
50056
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1119 self.assertRaises(BufferError, resize, 9)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60644
diff changeset
1120 self.assertEqual(b, orig)
50056
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1121 self.assertRaises(BufferError, resize, 0)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60644
diff changeset
1122 self.assertEqual(b, orig)
50056
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1123 # Other operations implying resize
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1124 self.assertRaises(BufferError, b.pop, 0)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60644
diff changeset
1125 self.assertEqual(b, orig)
50056
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1126 self.assertRaises(BufferError, b.remove, b[1])
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60644
diff changeset
1127 self.assertEqual(b, orig)
50056
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1128 def delitem():
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1129 del b[1]
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1130 self.assertRaises(BufferError, delitem)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60644
diff changeset
1131 self.assertEqual(b, orig)
50056
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1132 # deleting a non-contiguous slice
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1133 def delslice():
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1134 b[1:-1:2] = b""
1693fd94d829 Merged revisions 67618 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49819
diff changeset
1135 self.assertRaises(BufferError, delslice)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60644
diff changeset
1136 self.assertEqual(b, orig)
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43757
diff changeset
1137
58389
fb337cb46b9d Merged revisions 77576 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57026
diff changeset
1138
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1139 class AssortedBytesTest(unittest.TestCase):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1140 #
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1141 # Test various combinations of bytes and bytearray
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1142 #
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1143
64408
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
1144 @check_bytes_warnings
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1145 def test_repr_str(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1146 for f in str, repr:
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1147 self.assertEqual(f(bytearray()), "bytearray(b'')")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1148 self.assertEqual(f(bytearray([0])), "bytearray(b'\\x00')")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1149 self.assertEqual(f(bytearray([0, 1, 254, 255])),
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1150 "bytearray(b'\\x00\\x01\\xfe\\xff')")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1151 self.assertEqual(f(b"abc"), "b'abc'")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1152 self.assertEqual(f(b"'"), '''b"'"''') # '''
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1153 self.assertEqual(f(b"'\""), r"""b'\'"'""") # '
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1154
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1155 def test_compare_bytes_to_bytearray(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1156 self.assertEqual(b"abc" == bytes(b"abc"), True)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1157 self.assertEqual(b"ab" != bytes(b"abc"), True)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1158 self.assertEqual(b"ab" <= bytes(b"abc"), True)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1159 self.assertEqual(b"ab" < bytes(b"abc"), True)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1160 self.assertEqual(b"abc" >= bytes(b"ab"), True)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1161 self.assertEqual(b"abc" > bytes(b"ab"), True)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1162
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1163 self.assertEqual(b"abc" != bytes(b"abc"), False)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1164 self.assertEqual(b"ab" == bytes(b"abc"), False)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1165 self.assertEqual(b"ab" > bytes(b"abc"), False)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1166 self.assertEqual(b"ab" >= bytes(b"abc"), False)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1167 self.assertEqual(b"abc" < bytes(b"ab"), False)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1168 self.assertEqual(b"abc" <= bytes(b"ab"), False)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1169
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1170 self.assertEqual(bytes(b"abc") == b"abc", True)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1171 self.assertEqual(bytes(b"ab") != b"abc", True)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1172 self.assertEqual(bytes(b"ab") <= b"abc", True)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1173 self.assertEqual(bytes(b"ab") < b"abc", True)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1174 self.assertEqual(bytes(b"abc") >= b"ab", True)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1175 self.assertEqual(bytes(b"abc") > b"ab", True)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1176
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1177 self.assertEqual(bytes(b"abc") != b"abc", False)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1178 self.assertEqual(bytes(b"ab") == b"abc", False)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1179 self.assertEqual(bytes(b"ab") > b"abc", False)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1180 self.assertEqual(bytes(b"ab") >= b"abc", False)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1181 self.assertEqual(bytes(b"abc") < b"ab", False)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1182 self.assertEqual(bytes(b"abc") <= b"ab", False)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1183
81781
241b655c23b6 Issue #17041: Fix testing when Python is configured with the
Serhiy Storchaka <storchaka@gmail.com>
parents: 80209
diff changeset
1184 @test.support.requires_docstrings
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1185 def test_doc(self):
59728
4587e1f74ed8 Merged revisions 79100 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58563
diff changeset
1186 self.assertIsNotNone(bytearray.__doc__)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
1187 self.assertTrue(bytearray.__doc__.startswith("bytearray("), bytearray.__doc__)
59728
4587e1f74ed8 Merged revisions 79100 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58563
diff changeset
1188 self.assertIsNotNone(bytes.__doc__)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
1189 self.assertTrue(bytes.__doc__.startswith("bytes("), bytes.__doc__)
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1190
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1191 def test_from_bytearray(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1192 sample = bytes(b"Hello world\n\x80\x81\xfe\xff")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1193 buf = memoryview(sample)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1194 b = bytearray(buf)
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1195 self.assertEqual(b, bytearray(sample))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1196
64408
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
1197 @check_bytes_warnings
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1198 def test_to_str(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1199 self.assertEqual(str(b''), "b''")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1200 self.assertEqual(str(b'x'), "b'x'")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1201 self.assertEqual(str(b'\x80'), "b'\\x80'")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1202 self.assertEqual(str(bytearray(b'')), "bytearray(b'')")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1203 self.assertEqual(str(bytearray(b'x')), "bytearray(b'x')")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1204 self.assertEqual(str(bytearray(b'\x80')), "bytearray(b'\\x80')")
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1205
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1206 def test_literal(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1207 tests = [
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1208 (b"Wonderful spam", "Wonderful spam"),
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1209 (br"Wonderful spam too", "Wonderful spam too"),
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1210 (b"\xaa\x00\000\200", "\xaa\x00\000\200"),
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1211 (br"\xaa\x00\000\200", r"\xaa\x00\000\200"),
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1212 ]
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1213 for b, s in tests:
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1214 self.assertEqual(b, bytearray(s, 'latin-1'))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1215 for c in range(128, 256):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1216 self.assertRaises(SyntaxError, eval,
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1217 'b"%s"' % chr(c))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1218
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1219 def test_translate(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1220 b = b'hello'
50328
7a011680e151 #4759: allow None as first argument of bytearray.translate(), for consistency with bytes.translate().
Georg Brandl <georg@python.org>
parents: 50056
diff changeset
1221 ba = bytearray(b)
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1222 rosetta = bytearray(range(0, 256))
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1223 rosetta[ord('o')] = ord('e')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1224 c = b.translate(rosetta, b'l')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1225 self.assertEqual(b, b'hello')
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1226 self.assertEqual(c, b'hee')
50328
7a011680e151 #4759: allow None as first argument of bytearray.translate(), for consistency with bytes.translate().
Georg Brandl <georg@python.org>
parents: 50056
diff changeset
1227 c = ba.translate(rosetta, b'l')
7a011680e151 #4759: allow None as first argument of bytearray.translate(), for consistency with bytes.translate().
Georg Brandl <georg@python.org>
parents: 50056
diff changeset
1228 self.assertEqual(ba, b'hello')
7a011680e151 #4759: allow None as first argument of bytearray.translate(), for consistency with bytes.translate().
Georg Brandl <georg@python.org>
parents: 50056
diff changeset
1229 self.assertEqual(c, b'hee')
7a011680e151 #4759: allow None as first argument of bytearray.translate(), for consistency with bytes.translate().
Georg Brandl <georg@python.org>
parents: 50056
diff changeset
1230 c = b.translate(None, b'e')
7a011680e151 #4759: allow None as first argument of bytearray.translate(), for consistency with bytes.translate().
Georg Brandl <georg@python.org>
parents: 50056
diff changeset
1231 self.assertEqual(c, b'hllo')
7a011680e151 #4759: allow None as first argument of bytearray.translate(), for consistency with bytes.translate().
Georg Brandl <georg@python.org>
parents: 50056
diff changeset
1232 c = ba.translate(None, b'e')
7a011680e151 #4759: allow None as first argument of bytearray.translate(), for consistency with bytes.translate().
Georg Brandl <georg@python.org>
parents: 50056
diff changeset
1233 self.assertEqual(c, b'hllo')
7a011680e151 #4759: allow None as first argument of bytearray.translate(), for consistency with bytes.translate().
Georg Brandl <georg@python.org>
parents: 50056
diff changeset
1234 self.assertRaises(TypeError, b.translate, None, None)
7a011680e151 #4759: allow None as first argument of bytearray.translate(), for consistency with bytes.translate().
Georg Brandl <georg@python.org>
parents: 50056
diff changeset
1235 self.assertRaises(TypeError, ba.translate, None, None)
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1236
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1237 def test_split_bytearray(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1238 self.assertEqual(b'a b'.split(memoryview(b' ')), [b'a', b'b'])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1239
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1240 def test_rsplit_bytearray(self):
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1241 self.assertEqual(b'a b'.rsplit(memoryview(b' ')), [b'a', b'b'])
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1242
47391
eca882731194 Add test case for r63810.
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
1243 def test_return_self(self):
eca882731194 Add test case for r63810.
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
1244 # bytearray.replace must always return a new bytearray
eca882731194 Add test case for r63810.
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
1245 b = bytearray()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
1246 self.assertFalse(b.replace(b'', b'') is b)
47391
eca882731194 Add test case for r63810.
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
1247
49538
645fe56c117a STINNER Victor (haypo)'s patch for bug 3988, Byte warning mode and b'' != ''
Barry Warsaw <barry@python.org>
parents: 48871
diff changeset
1248 def test_compare(self):
645fe56c117a STINNER Victor (haypo)'s patch for bug 3988, Byte warning mode and b'' != ''
Barry Warsaw <barry@python.org>
parents: 48871
diff changeset
1249 if sys.flags.bytes_warning:
64408
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
1250 def bytes_warning():
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
1251 return test.support.check_warnings(('', BytesWarning))
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
1252 with bytes_warning():
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
1253 b'' == ''
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
1254 with bytes_warning():
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
1255 b'' != ''
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
1256 with bytes_warning():
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
1257 bytearray(b'') == ''
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
1258 with bytes_warning():
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
1259 bytearray(b'') != ''
49538
645fe56c117a STINNER Victor (haypo)'s patch for bug 3988, Byte warning mode and b'' != ''
Barry Warsaw <barry@python.org>
parents: 48871
diff changeset
1260 else:
64408
3f7764c40c0b Strengthen BytesWarning tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63913
diff changeset
1261 self.skipTest("BytesWarning is needed for this test: use -bb option")
49538
645fe56c117a STINNER Victor (haypo)'s patch for bug 3988, Byte warning mode and b'' != ''
Barry Warsaw <barry@python.org>
parents: 48871
diff changeset
1262
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
1263 # Optimizations:
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
1264 # __iter__? (optimization)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
1265 # __reversed__? (optimization)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
1266
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41425
diff changeset
1267 # XXX More string methods? (Those that don't use character properties)
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41425
diff changeset
1268
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1269 # There are tests in string_tests.py that are more
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1270 # comprehensive for things like split, partition, etc.
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1271 # Unfortunately they are all bundled with tests that
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40967
diff changeset
1272 # are not appropriate for bytes
37930
e1252cea439c Fix a leak and a buglet discovered by Thomas.
Guido van Rossum <guido@python.org>
parents: 37928
diff changeset
1273
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
1274 # I've started porting some of those into bytearray_tests.py, we should port
43576
697cc31a83f6 For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents: 43508
diff changeset
1275 # the rest that make sense (the code can be cleaned up to use modern
697cc31a83f6 For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents: 43508
diff changeset
1276 # unittest methods at the same time).
697cc31a83f6 For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents: 43508
diff changeset
1277
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
1278 class BytearrayPEP3137Test(unittest.TestCase,
43576
697cc31a83f6 For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents: 43508
diff changeset
1279 test.buffer_tests.MixinBytesBufferCommonTests):
697cc31a83f6 For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents: 43508
diff changeset
1280 def marshal(self, x):
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
1281 return bytearray(x)
43576
697cc31a83f6 For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents: 43508
diff changeset
1282
697cc31a83f6 For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents: 43508
diff changeset
1283 def test_returns_new_copy(self):
697cc31a83f6 For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents: 43508
diff changeset
1284 val = self.marshal(b'1234')
697cc31a83f6 For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents: 43508
diff changeset
1285 # On immutable types these MAY return a reference to themselves
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
1286 # but on mutable types like bytearray they MUST return a new copy.
43576
697cc31a83f6 For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents: 43508
diff changeset
1287 for methname in ('zfill', 'rjust', 'ljust', 'center'):
697cc31a83f6 For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents: 43508
diff changeset
1288 method = getattr(val, methname)
697cc31a83f6 For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents: 43508
diff changeset
1289 newval = method(3)
697cc31a83f6 For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents: 43508
diff changeset
1290 self.assertEqual(val, newval)
697cc31a83f6 For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents: 43508
diff changeset
1291 self.assertTrue(val is not newval,
697cc31a83f6 For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents: 43508
diff changeset
1292 methname+' returned self on a mutable object')
58303
9fc7f297d132 Merged revisions 77475 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58276
diff changeset
1293 for expr in ('val.split()[0]', 'val.rsplit()[0]',
9fc7f297d132 Merged revisions 77475 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58276
diff changeset
1294 'val.partition(b".")[0]', 'val.rpartition(b".")[2]',
9fc7f297d132 Merged revisions 77475 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58276
diff changeset
1295 'val.splitlines()[0]', 'val.replace(b"", b"")'):
9fc7f297d132 Merged revisions 77475 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58276
diff changeset
1296 newval = eval(expr)
9fc7f297d132 Merged revisions 77475 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58276
diff changeset
1297 self.assertEqual(val, newval)
9fc7f297d132 Merged revisions 77475 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58276
diff changeset
1298 self.assertTrue(val is not newval,
9fc7f297d132 Merged revisions 77475 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58276
diff changeset
1299 expr+' returned val on a mutable object')
79766
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
1300 sep = self.marshal(b'')
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
1301 newval = sep.join([val])
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
1302 self.assertEqual(val, newval)
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
1303 self.assertIsNot(val, newval)
16285c1b4dda Issue #15958: bytes.join and bytearray.join now accept arbitrary buffer objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
1304
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1305
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1306 class FixedStringTest(test.string_tests.BaseTest):
41038
efa0a761918c Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents: 40978
diff changeset
1307
42907
0170418dadea Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42499
diff changeset
1308 def fixtype(self, obj):
0170418dadea Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42499
diff changeset
1309 if isinstance(obj, str):
0170418dadea Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42499
diff changeset
1310 return obj.encode("utf-8")
0170418dadea Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42499
diff changeset
1311 return super().fixtype(obj)
0170418dadea Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42499
diff changeset
1312
41038
efa0a761918c Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents: 40978
diff changeset
1313 # Currently the bytes containment testing uses a single integer
efa0a761918c Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents: 40978
diff changeset
1314 # value. This may not be the final design, but until then the
efa0a761918c Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents: 40978
diff changeset
1315 # bytes section with in a bytes containment not valid
efa0a761918c Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents: 40978
diff changeset
1316 def test_contains(self):
efa0a761918c Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents: 40978
diff changeset
1317 pass
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41855
diff changeset
1318 def test_expandtabs(self):
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41855
diff changeset
1319 pass
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41855
diff changeset
1320 def test_upper(self):
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41855
diff changeset
1321 pass
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41855
diff changeset
1322 def test_lower(self):
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41855
diff changeset
1323 pass
41038
efa0a761918c Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents: 40978
diff changeset
1324
81356
55383912c225 #16910: test_bytes, test_unicode, and test_userstring now work with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80954
diff changeset
1325 class ByteArrayAsStringTest(FixedStringTest, unittest.TestCase):
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1326 type2test = bytearray
73038
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
1327 contains_bytes = True
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1328
81356
55383912c225 #16910: test_bytes, test_unicode, and test_userstring now work with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80954
diff changeset
1329 class BytesAsStringTest(FixedStringTest, unittest.TestCase):
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1330 type2test = bytes
73038
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 71543
diff changeset
1331 contains_bytes = True
45055
510a48289d1d Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents: 45050
diff changeset
1332
41038
efa0a761918c Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents: 40978
diff changeset
1333
81356
55383912c225 #16910: test_bytes, test_unicode, and test_userstring now work with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80954
diff changeset
1334 class SubclassTest:
43757
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1335
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1336 def test_basic(self):
58264
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1337 self.assertTrue(issubclass(self.subclass2test, self.type2test))
58473
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58456
diff changeset
1338 self.assertIsInstance(self.subclass2test(), self.type2test)
43757
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1339
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1340 a, b = b"abcd", b"efgh"
58264
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1341 _a, _b = self.subclass2test(a), self.subclass2test(b)
43757
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1342
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1343 # test comparison operators with subclass instances
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
1344 self.assertTrue(_a == _a)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
1345 self.assertTrue(_a != _b)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
1346 self.assertTrue(_a < _b)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
1347 self.assertTrue(_a <= _b)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
1348 self.assertTrue(_b >= _a)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
1349 self.assertTrue(_b > _a)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
1350 self.assertTrue(_a is not a)
43757
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1351
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1352 # test concat of subclass instances
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1353 self.assertEqual(a + b, _a + _b)
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1354 self.assertEqual(a + b, a + _b)
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1355 self.assertEqual(a + b, _a + b)
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1356
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1357 # test repeat
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
1358 self.assertTrue(a*5 == _a*5)
43757
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1359
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1360 def test_join(self):
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1361 # Make sure join returns a NEW object for single item sequences
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1362 # involving a subclass.
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1363 # Make sure that it is of the appropriate type.
58264
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1364 s1 = self.subclass2test(b"abcd")
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1365 s2 = self.type2test().join([s1])
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53681
diff changeset
1366 self.assertTrue(s1 is not s2)
58264
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1367 self.assertTrue(type(s2) is self.type2test, type(s2))
43757
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1368
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1369 # Test reverse, calling join on subclass
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1370 s3 = s1.join([b"abcd"])
58264
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1371 self.assertTrue(type(s3) is self.type2test)
43757
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1372
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1373 def test_pickle(self):
58264
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1374 a = self.subclass2test(b"abcd")
43757
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1375 a.x = 10
58264
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1376 a.y = self.subclass2test(b"efgh")
49574
bbab11cdb175 Issue #4183: Some tests didn't run with pickle.HIGHEST_PROTOCOL.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49538
diff changeset
1377 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
43757
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1378 b = pickle.loads(pickle.dumps(a, proto))
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1379 self.assertNotEqual(id(a), id(b))
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1380 self.assertEqual(a, b)
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1381 self.assertEqual(a.x, b.x)
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1382 self.assertEqual(a.y, b.y)
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1383 self.assertEqual(type(a), type(b))
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1384 self.assertEqual(type(a.y), type(b.y))
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1385
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1386 def test_copy(self):
58264
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1387 a = self.subclass2test(b"abcd")
43757
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1388 a.x = 10
58264
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1389 a.y = self.subclass2test(b"efgh")
43757
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1390 for copy_method in (copy.copy, copy.deepcopy):
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1391 b = copy_method(a)
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1392 self.assertNotEqual(id(a), id(b))
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1393 self.assertEqual(a, b)
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1394 self.assertEqual(a.x, b.x)
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1395 self.assertEqual(a.y, b.y)
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1396 self.assertEqual(type(a), type(b))
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1397 self.assertEqual(type(a.y), type(b.y))
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1398
58264
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1399
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1400 class ByteArraySubclass(bytearray):
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1401 pass
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1402
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1403 class BytesSubclass(bytes):
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1404 pass
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1405
81356
55383912c225 #16910: test_bytes, test_unicode, and test_userstring now work with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80954
diff changeset
1406 class ByteArraySubclassTest(SubclassTest, unittest.TestCase):
58264
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1407 type2test = bytearray
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1408 subclass2test = ByteArraySubclass
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1409
43757
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1410 def test_init_override(self):
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
1411 class subclass(bytearray):
58264
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1412 def __init__(me, newarg=1, *args, **kwargs):
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1413 bytearray.__init__(me, *args, **kwargs)
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1414 x = subclass(4, b"abcd")
43757
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1415 x = subclass(4, source=b"abcd")
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1416 self.assertEqual(x, b"abcd")
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1417 x = subclass(newarg=4, source=b"abcd")
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1418 self.assertEqual(x, b"abcd")
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1419
2ff0e81e68fc Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents: 43693
diff changeset
1420
81356
55383912c225 #16910: test_bytes, test_unicode, and test_userstring now work with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80954
diff changeset
1421 class BytesSubclassTest(SubclassTest, unittest.TestCase):
58264
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1422 type2test = bytes
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1423 subclass2test = BytesSubclass
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1424
efa5867d8a14 Issue #7382: Fix bytes.__getnewargs__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 57025
diff changeset
1425
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1426 if __name__ == "__main__":
81356
55383912c225 #16910: test_bytes, test_unicode, and test_userstring now work with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80954
diff changeset
1427 unittest.main()