1 | n/a | """Unit tests for the bytes and bytearray types. |
---|
2 | n/a | |
---|
3 | n/a | XXX This is a mess. Common tests should be unified with string_tests.py (and |
---|
4 | n/a | the latter should be modernized). |
---|
5 | n/a | """ |
---|
6 | n/a | |
---|
7 | n/a | import array |
---|
8 | n/a | import os |
---|
9 | n/a | import re |
---|
10 | n/a | import sys |
---|
11 | n/a | import copy |
---|
12 | n/a | import functools |
---|
13 | n/a | import pickle |
---|
14 | n/a | import tempfile |
---|
15 | n/a | import unittest |
---|
16 | n/a | |
---|
17 | n/a | import test.support |
---|
18 | n/a | import test.string_tests |
---|
19 | n/a | import test.list_tests |
---|
20 | n/a | from test.support import bigaddrspacetest, MAX_Py_ssize_t |
---|
21 | n/a | |
---|
22 | n/a | |
---|
23 | n/a | if sys.flags.bytes_warning: |
---|
24 | n/a | def check_bytes_warnings(func): |
---|
25 | n/a | @functools.wraps(func) |
---|
26 | n/a | def wrapper(*args, **kw): |
---|
27 | n/a | with test.support.check_warnings(('', BytesWarning)): |
---|
28 | n/a | return func(*args, **kw) |
---|
29 | n/a | return wrapper |
---|
30 | n/a | else: |
---|
31 | n/a | # no-op |
---|
32 | n/a | def check_bytes_warnings(func): |
---|
33 | n/a | return func |
---|
34 | n/a | |
---|
35 | n/a | |
---|
36 | n/a | class Indexable: |
---|
37 | n/a | def __init__(self, value=0): |
---|
38 | n/a | self.value = value |
---|
39 | n/a | def __index__(self): |
---|
40 | n/a | return self.value |
---|
41 | n/a | |
---|
42 | n/a | |
---|
43 | n/a | class BaseBytesTest: |
---|
44 | n/a | |
---|
45 | n/a | def test_basics(self): |
---|
46 | n/a | b = self.type2test() |
---|
47 | n/a | self.assertEqual(type(b), self.type2test) |
---|
48 | n/a | self.assertEqual(b.__class__, self.type2test) |
---|
49 | n/a | |
---|
50 | n/a | def test_copy(self): |
---|
51 | n/a | a = self.type2test(b"abcd") |
---|
52 | n/a | for copy_method in (copy.copy, copy.deepcopy): |
---|
53 | n/a | b = copy_method(a) |
---|
54 | n/a | self.assertEqual(a, b) |
---|
55 | n/a | self.assertEqual(type(a), type(b)) |
---|
56 | n/a | |
---|
57 | n/a | def test_empty_sequence(self): |
---|
58 | n/a | b = self.type2test() |
---|
59 | n/a | self.assertEqual(len(b), 0) |
---|
60 | n/a | self.assertRaises(IndexError, lambda: b[0]) |
---|
61 | n/a | self.assertRaises(IndexError, lambda: b[1]) |
---|
62 | n/a | self.assertRaises(IndexError, lambda: b[sys.maxsize]) |
---|
63 | n/a | self.assertRaises(IndexError, lambda: b[sys.maxsize+1]) |
---|
64 | n/a | self.assertRaises(IndexError, lambda: b[10**100]) |
---|
65 | n/a | self.assertRaises(IndexError, lambda: b[-1]) |
---|
66 | n/a | self.assertRaises(IndexError, lambda: b[-2]) |
---|
67 | n/a | self.assertRaises(IndexError, lambda: b[-sys.maxsize]) |
---|
68 | n/a | self.assertRaises(IndexError, lambda: b[-sys.maxsize-1]) |
---|
69 | n/a | self.assertRaises(IndexError, lambda: b[-sys.maxsize-2]) |
---|
70 | n/a | self.assertRaises(IndexError, lambda: b[-10**100]) |
---|
71 | n/a | |
---|
72 | n/a | def test_from_list(self): |
---|
73 | n/a | ints = list(range(256)) |
---|
74 | n/a | b = self.type2test(i for i in ints) |
---|
75 | n/a | self.assertEqual(len(b), 256) |
---|
76 | n/a | self.assertEqual(list(b), ints) |
---|
77 | n/a | |
---|
78 | n/a | def test_from_index(self): |
---|
79 | n/a | b = self.type2test([Indexable(), Indexable(1), Indexable(254), |
---|
80 | n/a | Indexable(255)]) |
---|
81 | n/a | self.assertEqual(list(b), [0, 1, 254, 255]) |
---|
82 | n/a | self.assertRaises(ValueError, self.type2test, [Indexable(-1)]) |
---|
83 | n/a | self.assertRaises(ValueError, self.type2test, [Indexable(256)]) |
---|
84 | n/a | |
---|
85 | n/a | def test_from_buffer(self): |
---|
86 | n/a | a = self.type2test(array.array('B', [1, 2, 3])) |
---|
87 | n/a | self.assertEqual(a, b"\x01\x02\x03") |
---|
88 | n/a | |
---|
89 | n/a | # http://bugs.python.org/issue29159 |
---|
90 | n/a | # Fallback when __index__ raises exception other than OverflowError |
---|
91 | n/a | class B(bytes): |
---|
92 | n/a | def __index__(self): |
---|
93 | n/a | raise TypeError |
---|
94 | n/a | |
---|
95 | n/a | self.assertEqual(self.type2test(B(b"foobar")), b"foobar") |
---|
96 | n/a | |
---|
97 | n/a | def test_from_ssize(self): |
---|
98 | n/a | self.assertEqual(self.type2test(0), b'') |
---|
99 | n/a | self.assertEqual(self.type2test(1), b'\x00') |
---|
100 | n/a | self.assertEqual(self.type2test(5), b'\x00\x00\x00\x00\x00') |
---|
101 | n/a | self.assertRaises(ValueError, self.type2test, -1) |
---|
102 | n/a | |
---|
103 | n/a | self.assertEqual(self.type2test('0', 'ascii'), b'0') |
---|
104 | n/a | self.assertEqual(self.type2test(b'0'), b'0') |
---|
105 | n/a | self.assertRaises(OverflowError, self.type2test, sys.maxsize + 1) |
---|
106 | n/a | |
---|
107 | n/a | def test_constructor_type_errors(self): |
---|
108 | n/a | self.assertRaises(TypeError, self.type2test, 0.0) |
---|
109 | n/a | class C: |
---|
110 | n/a | pass |
---|
111 | n/a | self.assertRaises(TypeError, self.type2test, ["0"]) |
---|
112 | n/a | self.assertRaises(TypeError, self.type2test, [0.0]) |
---|
113 | n/a | self.assertRaises(TypeError, self.type2test, [None]) |
---|
114 | n/a | self.assertRaises(TypeError, self.type2test, [C()]) |
---|
115 | n/a | self.assertRaises(TypeError, self.type2test, 0, 'ascii') |
---|
116 | n/a | self.assertRaises(TypeError, self.type2test, b'', 'ascii') |
---|
117 | n/a | self.assertRaises(TypeError, self.type2test, 0, errors='ignore') |
---|
118 | n/a | self.assertRaises(TypeError, self.type2test, b'', errors='ignore') |
---|
119 | n/a | self.assertRaises(TypeError, self.type2test, '') |
---|
120 | n/a | self.assertRaises(TypeError, self.type2test, '', errors='ignore') |
---|
121 | n/a | self.assertRaises(TypeError, self.type2test, '', b'ascii') |
---|
122 | n/a | self.assertRaises(TypeError, self.type2test, '', 'ascii', b'ignore') |
---|
123 | n/a | |
---|
124 | n/a | def test_constructor_value_errors(self): |
---|
125 | n/a | self.assertRaises(ValueError, self.type2test, [-1]) |
---|
126 | n/a | self.assertRaises(ValueError, self.type2test, [-sys.maxsize]) |
---|
127 | n/a | self.assertRaises(ValueError, self.type2test, [-sys.maxsize-1]) |
---|
128 | n/a | self.assertRaises(ValueError, self.type2test, [-sys.maxsize-2]) |
---|
129 | n/a | self.assertRaises(ValueError, self.type2test, [-10**100]) |
---|
130 | n/a | self.assertRaises(ValueError, self.type2test, [256]) |
---|
131 | n/a | self.assertRaises(ValueError, self.type2test, [257]) |
---|
132 | n/a | self.assertRaises(ValueError, self.type2test, [sys.maxsize]) |
---|
133 | n/a | self.assertRaises(ValueError, self.type2test, [sys.maxsize+1]) |
---|
134 | n/a | self.assertRaises(ValueError, self.type2test, [10**100]) |
---|
135 | n/a | |
---|
136 | n/a | @bigaddrspacetest |
---|
137 | n/a | def test_constructor_overflow(self): |
---|
138 | n/a | size = MAX_Py_ssize_t |
---|
139 | n/a | self.assertRaises((OverflowError, MemoryError), self.type2test, size) |
---|
140 | n/a | try: |
---|
141 | n/a | # Should either pass or raise an error (e.g. on debug builds with |
---|
142 | n/a | # additional malloc() overhead), but shouldn't crash. |
---|
143 | n/a | bytearray(size - 4) |
---|
144 | n/a | except (OverflowError, MemoryError): |
---|
145 | n/a | pass |
---|
146 | n/a | |
---|
147 | n/a | def test_compare(self): |
---|
148 | n/a | b1 = self.type2test([1, 2, 3]) |
---|
149 | n/a | b2 = self.type2test([1, 2, 3]) |
---|
150 | n/a | b3 = self.type2test([1, 3]) |
---|
151 | n/a | |
---|
152 | n/a | self.assertEqual(b1, b2) |
---|
153 | n/a | self.assertTrue(b2 != b3) |
---|
154 | n/a | self.assertTrue(b1 <= b2) |
---|
155 | n/a | self.assertTrue(b1 <= b3) |
---|
156 | n/a | self.assertTrue(b1 < b3) |
---|
157 | n/a | self.assertTrue(b1 >= b2) |
---|
158 | n/a | self.assertTrue(b3 >= b2) |
---|
159 | n/a | self.assertTrue(b3 > b2) |
---|
160 | n/a | |
---|
161 | n/a | self.assertFalse(b1 != b2) |
---|
162 | n/a | self.assertFalse(b2 == b3) |
---|
163 | n/a | self.assertFalse(b1 > b2) |
---|
164 | n/a | self.assertFalse(b1 > b3) |
---|
165 | n/a | self.assertFalse(b1 >= b3) |
---|
166 | n/a | self.assertFalse(b1 < b2) |
---|
167 | n/a | self.assertFalse(b3 < b2) |
---|
168 | n/a | self.assertFalse(b3 <= b2) |
---|
169 | n/a | |
---|
170 | n/a | @check_bytes_warnings |
---|
171 | n/a | def test_compare_to_str(self): |
---|
172 | n/a | # Byte comparisons with unicode should always fail! |
---|
173 | n/a | # Test this for all expected byte orders and Unicode character |
---|
174 | n/a | # sizes. |
---|
175 | n/a | self.assertEqual(self.type2test(b"\0a\0b\0c") == "abc", False) |
---|
176 | n/a | self.assertEqual(self.type2test(b"\0\0\0a\0\0\0b\0\0\0c") == "abc", |
---|
177 | n/a | False) |
---|
178 | n/a | self.assertEqual(self.type2test(b"a\0b\0c\0") == "abc", False) |
---|
179 | n/a | self.assertEqual(self.type2test(b"a\0\0\0b\0\0\0c\0\0\0") == "abc", |
---|
180 | n/a | False) |
---|
181 | n/a | self.assertEqual(self.type2test() == str(), False) |
---|
182 | n/a | self.assertEqual(self.type2test() != str(), True) |
---|
183 | n/a | |
---|
184 | n/a | def test_reversed(self): |
---|
185 | n/a | input = list(map(ord, "Hello")) |
---|
186 | n/a | b = self.type2test(input) |
---|
187 | n/a | output = list(reversed(b)) |
---|
188 | n/a | input.reverse() |
---|
189 | n/a | self.assertEqual(output, input) |
---|
190 | n/a | |
---|
191 | n/a | def test_getslice(self): |
---|
192 | n/a | def by(s): |
---|
193 | n/a | return self.type2test(map(ord, s)) |
---|
194 | n/a | b = by("Hello, world") |
---|
195 | n/a | |
---|
196 | n/a | self.assertEqual(b[:5], by("Hello")) |
---|
197 | n/a | self.assertEqual(b[1:5], by("ello")) |
---|
198 | n/a | self.assertEqual(b[5:7], by(", ")) |
---|
199 | n/a | self.assertEqual(b[7:], by("world")) |
---|
200 | n/a | self.assertEqual(b[7:12], by("world")) |
---|
201 | n/a | self.assertEqual(b[7:100], by("world")) |
---|
202 | n/a | |
---|
203 | n/a | self.assertEqual(b[:-7], by("Hello")) |
---|
204 | n/a | self.assertEqual(b[-11:-7], by("ello")) |
---|
205 | n/a | self.assertEqual(b[-7:-5], by(", ")) |
---|
206 | n/a | self.assertEqual(b[-5:], by("world")) |
---|
207 | n/a | self.assertEqual(b[-5:12], by("world")) |
---|
208 | n/a | self.assertEqual(b[-5:100], by("world")) |
---|
209 | n/a | self.assertEqual(b[-100:5], by("Hello")) |
---|
210 | n/a | |
---|
211 | n/a | def test_extended_getslice(self): |
---|
212 | n/a | # Test extended slicing by comparing with list slicing. |
---|
213 | n/a | L = list(range(255)) |
---|
214 | n/a | b = self.type2test(L) |
---|
215 | n/a | indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100) |
---|
216 | n/a | for start in indices: |
---|
217 | n/a | for stop in indices: |
---|
218 | n/a | # Skip step 0 (invalid) |
---|
219 | n/a | for step in indices[1:]: |
---|
220 | n/a | self.assertEqual(b[start:stop:step], self.type2test(L[start:stop:step])) |
---|
221 | n/a | |
---|
222 | n/a | def test_encoding(self): |
---|
223 | n/a | sample = "Hello world\n\u1234\u5678\u9abc" |
---|
224 | n/a | for enc in ("utf-8", "utf-16"): |
---|
225 | n/a | b = self.type2test(sample, enc) |
---|
226 | n/a | self.assertEqual(b, self.type2test(sample.encode(enc))) |
---|
227 | n/a | self.assertRaises(UnicodeEncodeError, self.type2test, sample, "latin-1") |
---|
228 | n/a | b = self.type2test(sample, "latin-1", "ignore") |
---|
229 | n/a | self.assertEqual(b, self.type2test(sample[:-3], "utf-8")) |
---|
230 | n/a | |
---|
231 | n/a | def test_decode(self): |
---|
232 | n/a | sample = "Hello world\n\u1234\u5678\u9abc" |
---|
233 | n/a | for enc in ("utf-8", "utf-16"): |
---|
234 | n/a | b = self.type2test(sample, enc) |
---|
235 | n/a | self.assertEqual(b.decode(enc), sample) |
---|
236 | n/a | sample = "Hello world\n\x80\x81\xfe\xff" |
---|
237 | n/a | b = self.type2test(sample, "latin-1") |
---|
238 | n/a | self.assertRaises(UnicodeDecodeError, b.decode, "utf-8") |
---|
239 | n/a | self.assertEqual(b.decode("utf-8", "ignore"), "Hello world\n") |
---|
240 | n/a | self.assertEqual(b.decode(errors="ignore", encoding="utf-8"), |
---|
241 | n/a | "Hello world\n") |
---|
242 | n/a | # Default encoding is utf-8 |
---|
243 | n/a | self.assertEqual(self.type2test(b'\xe2\x98\x83').decode(), '\u2603') |
---|
244 | n/a | |
---|
245 | n/a | def test_from_int(self): |
---|
246 | n/a | b = self.type2test(0) |
---|
247 | n/a | self.assertEqual(b, self.type2test()) |
---|
248 | n/a | b = self.type2test(10) |
---|
249 | n/a | self.assertEqual(b, self.type2test([0]*10)) |
---|
250 | n/a | b = self.type2test(10000) |
---|
251 | n/a | self.assertEqual(b, self.type2test([0]*10000)) |
---|
252 | n/a | |
---|
253 | n/a | def test_concat(self): |
---|
254 | n/a | b1 = self.type2test(b"abc") |
---|
255 | n/a | b2 = self.type2test(b"def") |
---|
256 | n/a | self.assertEqual(b1 + b2, b"abcdef") |
---|
257 | n/a | self.assertEqual(b1 + bytes(b"def"), b"abcdef") |
---|
258 | n/a | self.assertEqual(bytes(b"def") + b1, b"defabc") |
---|
259 | n/a | self.assertRaises(TypeError, lambda: b1 + "def") |
---|
260 | n/a | self.assertRaises(TypeError, lambda: "abc" + b2) |
---|
261 | n/a | |
---|
262 | n/a | def test_repeat(self): |
---|
263 | n/a | for b in b"abc", self.type2test(b"abc"): |
---|
264 | n/a | self.assertEqual(b * 3, b"abcabcabc") |
---|
265 | n/a | self.assertEqual(b * 0, b"") |
---|
266 | n/a | self.assertEqual(b * -1, b"") |
---|
267 | n/a | self.assertRaises(TypeError, lambda: b * 3.14) |
---|
268 | n/a | self.assertRaises(TypeError, lambda: 3.14 * b) |
---|
269 | n/a | # XXX Shouldn't bytes and bytearray agree on what to raise? |
---|
270 | n/a | with self.assertRaises((OverflowError, MemoryError)): |
---|
271 | n/a | c = b * sys.maxsize |
---|
272 | n/a | with self.assertRaises((OverflowError, MemoryError)): |
---|
273 | n/a | b *= sys.maxsize |
---|
274 | n/a | |
---|
275 | n/a | def test_repeat_1char(self): |
---|
276 | n/a | self.assertEqual(self.type2test(b'x')*100, self.type2test([ord('x')]*100)) |
---|
277 | n/a | |
---|
278 | n/a | def test_contains(self): |
---|
279 | n/a | b = self.type2test(b"abc") |
---|
280 | n/a | self.assertIn(ord('a'), b) |
---|
281 | n/a | self.assertIn(int(ord('a')), b) |
---|
282 | n/a | self.assertNotIn(200, b) |
---|
283 | n/a | self.assertRaises(ValueError, lambda: 300 in b) |
---|
284 | n/a | self.assertRaises(ValueError, lambda: -1 in b) |
---|
285 | n/a | self.assertRaises(ValueError, lambda: sys.maxsize+1 in b) |
---|
286 | n/a | self.assertRaises(TypeError, lambda: None in b) |
---|
287 | n/a | self.assertRaises(TypeError, lambda: float(ord('a')) in b) |
---|
288 | n/a | self.assertRaises(TypeError, lambda: "a" in b) |
---|
289 | n/a | for f in bytes, bytearray: |
---|
290 | n/a | self.assertIn(f(b""), b) |
---|
291 | n/a | self.assertIn(f(b"a"), b) |
---|
292 | n/a | self.assertIn(f(b"b"), b) |
---|
293 | n/a | self.assertIn(f(b"c"), b) |
---|
294 | n/a | self.assertIn(f(b"ab"), b) |
---|
295 | n/a | self.assertIn(f(b"bc"), b) |
---|
296 | n/a | self.assertIn(f(b"abc"), b) |
---|
297 | n/a | self.assertNotIn(f(b"ac"), b) |
---|
298 | n/a | self.assertNotIn(f(b"d"), b) |
---|
299 | n/a | self.assertNotIn(f(b"dab"), b) |
---|
300 | n/a | self.assertNotIn(f(b"abd"), b) |
---|
301 | n/a | |
---|
302 | n/a | def test_fromhex(self): |
---|
303 | n/a | self.assertRaises(TypeError, self.type2test.fromhex) |
---|
304 | n/a | self.assertRaises(TypeError, self.type2test.fromhex, 1) |
---|
305 | n/a | self.assertEqual(self.type2test.fromhex(''), self.type2test()) |
---|
306 | n/a | b = bytearray([0x1a, 0x2b, 0x30]) |
---|
307 | n/a | self.assertEqual(self.type2test.fromhex('1a2B30'), b) |
---|
308 | n/a | self.assertEqual(self.type2test.fromhex(' 1A 2B 30 '), b) |
---|
309 | n/a | |
---|
310 | n/a | # check that ASCII whitespace is ignored |
---|
311 | n/a | self.assertEqual(self.type2test.fromhex(' 1A\n2B\t30\v'), b) |
---|
312 | n/a | for c in "\x09\x0A\x0B\x0C\x0D\x20": |
---|
313 | n/a | self.assertEqual(self.type2test.fromhex(c), self.type2test()) |
---|
314 | n/a | for c in "\x1C\x1D\x1E\x1F\x85\xa0\u2000\u2002\u2028": |
---|
315 | n/a | self.assertRaises(ValueError, self.type2test.fromhex, c) |
---|
316 | n/a | |
---|
317 | n/a | self.assertEqual(self.type2test.fromhex('0000'), b'\0\0') |
---|
318 | n/a | self.assertRaises(TypeError, self.type2test.fromhex, b'1B') |
---|
319 | n/a | self.assertRaises(ValueError, self.type2test.fromhex, 'a') |
---|
320 | n/a | self.assertRaises(ValueError, self.type2test.fromhex, 'rt') |
---|
321 | n/a | self.assertRaises(ValueError, self.type2test.fromhex, '1a b cd') |
---|
322 | n/a | self.assertRaises(ValueError, self.type2test.fromhex, '\x00') |
---|
323 | n/a | self.assertRaises(ValueError, self.type2test.fromhex, '12 \x00 34') |
---|
324 | n/a | |
---|
325 | n/a | for data, pos in ( |
---|
326 | n/a | # invalid first hexadecimal character |
---|
327 | n/a | ('12 x4 56', 3), |
---|
328 | n/a | # invalid second hexadecimal character |
---|
329 | n/a | ('12 3x 56', 4), |
---|
330 | n/a | # two invalid hexadecimal characters |
---|
331 | n/a | ('12 xy 56', 3), |
---|
332 | n/a | # test non-ASCII string |
---|
333 | n/a | ('12 3\xff 56', 4), |
---|
334 | n/a | ): |
---|
335 | n/a | with self.assertRaises(ValueError) as cm: |
---|
336 | n/a | self.type2test.fromhex(data) |
---|
337 | n/a | self.assertIn('at position %s' % pos, str(cm.exception)) |
---|
338 | n/a | |
---|
339 | n/a | def test_hex(self): |
---|
340 | n/a | self.assertRaises(TypeError, self.type2test.hex) |
---|
341 | n/a | self.assertRaises(TypeError, self.type2test.hex, 1) |
---|
342 | n/a | self.assertEqual(self.type2test(b"").hex(), "") |
---|
343 | n/a | self.assertEqual(bytearray([0x1a, 0x2b, 0x30]).hex(), '1a2b30') |
---|
344 | n/a | self.assertEqual(self.type2test(b"\x1a\x2b\x30").hex(), '1a2b30') |
---|
345 | n/a | self.assertEqual(memoryview(b"\x1a\x2b\x30").hex(), '1a2b30') |
---|
346 | n/a | |
---|
347 | n/a | def test_join(self): |
---|
348 | n/a | self.assertEqual(self.type2test(b"").join([]), b"") |
---|
349 | n/a | self.assertEqual(self.type2test(b"").join([b""]), b"") |
---|
350 | n/a | for lst in [[b"abc"], [b"a", b"bc"], [b"ab", b"c"], [b"a", b"b", b"c"]]: |
---|
351 | n/a | lst = list(map(self.type2test, lst)) |
---|
352 | n/a | self.assertEqual(self.type2test(b"").join(lst), b"abc") |
---|
353 | n/a | self.assertEqual(self.type2test(b"").join(tuple(lst)), b"abc") |
---|
354 | n/a | self.assertEqual(self.type2test(b"").join(iter(lst)), b"abc") |
---|
355 | n/a | dot_join = self.type2test(b".:").join |
---|
356 | n/a | self.assertEqual(dot_join([b"ab", b"cd"]), b"ab.:cd") |
---|
357 | n/a | self.assertEqual(dot_join([memoryview(b"ab"), b"cd"]), b"ab.:cd") |
---|
358 | n/a | self.assertEqual(dot_join([b"ab", memoryview(b"cd")]), b"ab.:cd") |
---|
359 | n/a | self.assertEqual(dot_join([bytearray(b"ab"), b"cd"]), b"ab.:cd") |
---|
360 | n/a | self.assertEqual(dot_join([b"ab", bytearray(b"cd")]), b"ab.:cd") |
---|
361 | n/a | # Stress it with many items |
---|
362 | n/a | seq = [b"abc"] * 1000 |
---|
363 | n/a | expected = b"abc" + b".:abc" * 999 |
---|
364 | n/a | self.assertEqual(dot_join(seq), expected) |
---|
365 | n/a | self.assertRaises(TypeError, self.type2test(b" ").join, None) |
---|
366 | n/a | # Error handling and cleanup when some item in the middle of the |
---|
367 | n/a | # sequence has the wrong type. |
---|
368 | n/a | with self.assertRaises(TypeError): |
---|
369 | n/a | dot_join([bytearray(b"ab"), "cd", b"ef"]) |
---|
370 | n/a | with self.assertRaises(TypeError): |
---|
371 | n/a | dot_join([memoryview(b"ab"), "cd", b"ef"]) |
---|
372 | n/a | |
---|
373 | n/a | def test_count(self): |
---|
374 | n/a | b = self.type2test(b'mississippi') |
---|
375 | n/a | i = 105 |
---|
376 | n/a | p = 112 |
---|
377 | n/a | w = 119 |
---|
378 | n/a | |
---|
379 | n/a | self.assertEqual(b.count(b'i'), 4) |
---|
380 | n/a | self.assertEqual(b.count(b'ss'), 2) |
---|
381 | n/a | self.assertEqual(b.count(b'w'), 0) |
---|
382 | n/a | |
---|
383 | n/a | self.assertEqual(b.count(i), 4) |
---|
384 | n/a | self.assertEqual(b.count(w), 0) |
---|
385 | n/a | |
---|
386 | n/a | self.assertEqual(b.count(b'i', 6), 2) |
---|
387 | n/a | self.assertEqual(b.count(b'p', 6), 2) |
---|
388 | n/a | self.assertEqual(b.count(b'i', 1, 3), 1) |
---|
389 | n/a | self.assertEqual(b.count(b'p', 7, 9), 1) |
---|
390 | n/a | |
---|
391 | n/a | self.assertEqual(b.count(i, 6), 2) |
---|
392 | n/a | self.assertEqual(b.count(p, 6), 2) |
---|
393 | n/a | self.assertEqual(b.count(i, 1, 3), 1) |
---|
394 | n/a | self.assertEqual(b.count(p, 7, 9), 1) |
---|
395 | n/a | |
---|
396 | n/a | def test_startswith(self): |
---|
397 | n/a | b = self.type2test(b'hello') |
---|
398 | n/a | self.assertFalse(self.type2test().startswith(b"anything")) |
---|
399 | n/a | self.assertTrue(b.startswith(b"hello")) |
---|
400 | n/a | self.assertTrue(b.startswith(b"hel")) |
---|
401 | n/a | self.assertTrue(b.startswith(b"h")) |
---|
402 | n/a | self.assertFalse(b.startswith(b"hellow")) |
---|
403 | n/a | self.assertFalse(b.startswith(b"ha")) |
---|
404 | n/a | with self.assertRaises(TypeError) as cm: |
---|
405 | n/a | b.startswith([b'h']) |
---|
406 | n/a | exc = str(cm.exception) |
---|
407 | n/a | self.assertIn('bytes', exc) |
---|
408 | n/a | self.assertIn('tuple', exc) |
---|
409 | n/a | |
---|
410 | n/a | def test_endswith(self): |
---|
411 | n/a | b = self.type2test(b'hello') |
---|
412 | n/a | self.assertFalse(bytearray().endswith(b"anything")) |
---|
413 | n/a | self.assertTrue(b.endswith(b"hello")) |
---|
414 | n/a | self.assertTrue(b.endswith(b"llo")) |
---|
415 | n/a | self.assertTrue(b.endswith(b"o")) |
---|
416 | n/a | self.assertFalse(b.endswith(b"whello")) |
---|
417 | n/a | self.assertFalse(b.endswith(b"no")) |
---|
418 | n/a | with self.assertRaises(TypeError) as cm: |
---|
419 | n/a | b.endswith([b'o']) |
---|
420 | n/a | exc = str(cm.exception) |
---|
421 | n/a | self.assertIn('bytes', exc) |
---|
422 | n/a | self.assertIn('tuple', exc) |
---|
423 | n/a | |
---|
424 | n/a | def test_find(self): |
---|
425 | n/a | b = self.type2test(b'mississippi') |
---|
426 | n/a | i = 105 |
---|
427 | n/a | w = 119 |
---|
428 | n/a | |
---|
429 | n/a | self.assertEqual(b.find(b'ss'), 2) |
---|
430 | n/a | self.assertEqual(b.find(b'w'), -1) |
---|
431 | n/a | self.assertEqual(b.find(b'mississippian'), -1) |
---|
432 | n/a | |
---|
433 | n/a | self.assertEqual(b.find(i), 1) |
---|
434 | n/a | self.assertEqual(b.find(w), -1) |
---|
435 | n/a | |
---|
436 | n/a | self.assertEqual(b.find(b'ss', 3), 5) |
---|
437 | n/a | self.assertEqual(b.find(b'ss', 1, 7), 2) |
---|
438 | n/a | self.assertEqual(b.find(b'ss', 1, 3), -1) |
---|
439 | n/a | |
---|
440 | n/a | self.assertEqual(b.find(i, 6), 7) |
---|
441 | n/a | self.assertEqual(b.find(i, 1, 3), 1) |
---|
442 | n/a | self.assertEqual(b.find(w, 1, 3), -1) |
---|
443 | n/a | |
---|
444 | n/a | for index in (-1, 256, sys.maxsize + 1): |
---|
445 | n/a | self.assertRaisesRegex( |
---|
446 | n/a | ValueError, r'byte must be in range\(0, 256\)', |
---|
447 | n/a | b.find, index) |
---|
448 | n/a | |
---|
449 | n/a | def test_rfind(self): |
---|
450 | n/a | b = self.type2test(b'mississippi') |
---|
451 | n/a | i = 105 |
---|
452 | n/a | w = 119 |
---|
453 | n/a | |
---|
454 | n/a | self.assertEqual(b.rfind(b'ss'), 5) |
---|
455 | n/a | self.assertEqual(b.rfind(b'w'), -1) |
---|
456 | n/a | self.assertEqual(b.rfind(b'mississippian'), -1) |
---|
457 | n/a | |
---|
458 | n/a | self.assertEqual(b.rfind(i), 10) |
---|
459 | n/a | self.assertEqual(b.rfind(w), -1) |
---|
460 | n/a | |
---|
461 | n/a | self.assertEqual(b.rfind(b'ss', 3), 5) |
---|
462 | n/a | self.assertEqual(b.rfind(b'ss', 0, 6), 2) |
---|
463 | n/a | |
---|
464 | n/a | self.assertEqual(b.rfind(i, 1, 3), 1) |
---|
465 | n/a | self.assertEqual(b.rfind(i, 3, 9), 7) |
---|
466 | n/a | self.assertEqual(b.rfind(w, 1, 3), -1) |
---|
467 | n/a | |
---|
468 | n/a | def test_index(self): |
---|
469 | n/a | b = self.type2test(b'mississippi') |
---|
470 | n/a | i = 105 |
---|
471 | n/a | w = 119 |
---|
472 | n/a | |
---|
473 | n/a | self.assertEqual(b.index(b'ss'), 2) |
---|
474 | n/a | self.assertRaises(ValueError, b.index, b'w') |
---|
475 | n/a | self.assertRaises(ValueError, b.index, b'mississippian') |
---|
476 | n/a | |
---|
477 | n/a | self.assertEqual(b.index(i), 1) |
---|
478 | n/a | self.assertRaises(ValueError, b.index, w) |
---|
479 | n/a | |
---|
480 | n/a | self.assertEqual(b.index(b'ss', 3), 5) |
---|
481 | n/a | self.assertEqual(b.index(b'ss', 1, 7), 2) |
---|
482 | n/a | self.assertRaises(ValueError, b.index, b'ss', 1, 3) |
---|
483 | n/a | |
---|
484 | n/a | self.assertEqual(b.index(i, 6), 7) |
---|
485 | n/a | self.assertEqual(b.index(i, 1, 3), 1) |
---|
486 | n/a | self.assertRaises(ValueError, b.index, w, 1, 3) |
---|
487 | n/a | |
---|
488 | n/a | def test_rindex(self): |
---|
489 | n/a | b = self.type2test(b'mississippi') |
---|
490 | n/a | i = 105 |
---|
491 | n/a | w = 119 |
---|
492 | n/a | |
---|
493 | n/a | self.assertEqual(b.rindex(b'ss'), 5) |
---|
494 | n/a | self.assertRaises(ValueError, b.rindex, b'w') |
---|
495 | n/a | self.assertRaises(ValueError, b.rindex, b'mississippian') |
---|
496 | n/a | |
---|
497 | n/a | self.assertEqual(b.rindex(i), 10) |
---|
498 | n/a | self.assertRaises(ValueError, b.rindex, w) |
---|
499 | n/a | |
---|
500 | n/a | self.assertEqual(b.rindex(b'ss', 3), 5) |
---|
501 | n/a | self.assertEqual(b.rindex(b'ss', 0, 6), 2) |
---|
502 | n/a | |
---|
503 | n/a | self.assertEqual(b.rindex(i, 1, 3), 1) |
---|
504 | n/a | self.assertEqual(b.rindex(i, 3, 9), 7) |
---|
505 | n/a | self.assertRaises(ValueError, b.rindex, w, 1, 3) |
---|
506 | n/a | |
---|
507 | n/a | def test_mod(self): |
---|
508 | n/a | b = self.type2test(b'hello, %b!') |
---|
509 | n/a | orig = b |
---|
510 | n/a | b = b % b'world' |
---|
511 | n/a | self.assertEqual(b, b'hello, world!') |
---|
512 | n/a | self.assertEqual(orig, b'hello, %b!') |
---|
513 | n/a | self.assertFalse(b is orig) |
---|
514 | n/a | b = self.type2test(b'%s / 100 = %d%%') |
---|
515 | n/a | a = b % (b'seventy-nine', 79) |
---|
516 | n/a | self.assertEqual(a, b'seventy-nine / 100 = 79%') |
---|
517 | n/a | self.assertIs(type(a), self.type2test) |
---|
518 | n/a | |
---|
519 | n/a | def test_imod(self): |
---|
520 | n/a | b = self.type2test(b'hello, %b!') |
---|
521 | n/a | orig = b |
---|
522 | n/a | b %= b'world' |
---|
523 | n/a | self.assertEqual(b, b'hello, world!') |
---|
524 | n/a | self.assertEqual(orig, b'hello, %b!') |
---|
525 | n/a | self.assertFalse(b is orig) |
---|
526 | n/a | b = self.type2test(b'%s / 100 = %d%%') |
---|
527 | n/a | b %= (b'seventy-nine', 79) |
---|
528 | n/a | self.assertEqual(b, b'seventy-nine / 100 = 79%') |
---|
529 | n/a | self.assertIs(type(b), self.type2test) |
---|
530 | n/a | |
---|
531 | n/a | def test_rmod(self): |
---|
532 | n/a | with self.assertRaises(TypeError): |
---|
533 | n/a | object() % self.type2test(b'abc') |
---|
534 | n/a | self.assertIs(self.type2test(b'abc').__rmod__('%r'), NotImplemented) |
---|
535 | n/a | |
---|
536 | n/a | def test_replace(self): |
---|
537 | n/a | b = self.type2test(b'mississippi') |
---|
538 | n/a | self.assertEqual(b.replace(b'i', b'a'), b'massassappa') |
---|
539 | n/a | self.assertEqual(b.replace(b'ss', b'x'), b'mixixippi') |
---|
540 | n/a | |
---|
541 | n/a | def test_split_string_error(self): |
---|
542 | n/a | self.assertRaises(TypeError, self.type2test(b'a b').split, ' ') |
---|
543 | n/a | |
---|
544 | n/a | def test_split_unicodewhitespace(self): |
---|
545 | n/a | for b in (b'a\x1Cb', b'a\x1Db', b'a\x1Eb', b'a\x1Fb'): |
---|
546 | n/a | b = self.type2test(b) |
---|
547 | n/a | self.assertEqual(b.split(), [b]) |
---|
548 | n/a | b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F") |
---|
549 | n/a | self.assertEqual(b.split(), [b'\x1c\x1d\x1e\x1f']) |
---|
550 | n/a | |
---|
551 | n/a | def test_rsplit_string_error(self): |
---|
552 | n/a | self.assertRaises(TypeError, self.type2test(b'a b').rsplit, ' ') |
---|
553 | n/a | |
---|
554 | n/a | def test_rsplit_unicodewhitespace(self): |
---|
555 | n/a | b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F") |
---|
556 | n/a | self.assertEqual(b.rsplit(), [b'\x1c\x1d\x1e\x1f']) |
---|
557 | n/a | |
---|
558 | n/a | def test_partition(self): |
---|
559 | n/a | b = self.type2test(b'mississippi') |
---|
560 | n/a | self.assertEqual(b.partition(b'ss'), (b'mi', b'ss', b'issippi')) |
---|
561 | n/a | self.assertEqual(b.partition(b'w'), (b'mississippi', b'', b'')) |
---|
562 | n/a | |
---|
563 | n/a | def test_rpartition(self): |
---|
564 | n/a | b = self.type2test(b'mississippi') |
---|
565 | n/a | self.assertEqual(b.rpartition(b'ss'), (b'missi', b'ss', b'ippi')) |
---|
566 | n/a | self.assertEqual(b.rpartition(b'i'), (b'mississipp', b'i', b'')) |
---|
567 | n/a | self.assertEqual(b.rpartition(b'w'), (b'', b'', b'mississippi')) |
---|
568 | n/a | |
---|
569 | n/a | def test_pickling(self): |
---|
570 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
571 | n/a | for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0": |
---|
572 | n/a | b = self.type2test(b) |
---|
573 | n/a | ps = pickle.dumps(b, proto) |
---|
574 | n/a | q = pickle.loads(ps) |
---|
575 | n/a | self.assertEqual(b, q) |
---|
576 | n/a | |
---|
577 | n/a | def test_iterator_pickling(self): |
---|
578 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
579 | n/a | for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0": |
---|
580 | n/a | it = itorg = iter(self.type2test(b)) |
---|
581 | n/a | data = list(self.type2test(b)) |
---|
582 | n/a | d = pickle.dumps(it, proto) |
---|
583 | n/a | it = pickle.loads(d) |
---|
584 | n/a | self.assertEqual(type(itorg), type(it)) |
---|
585 | n/a | self.assertEqual(list(it), data) |
---|
586 | n/a | |
---|
587 | n/a | it = pickle.loads(d) |
---|
588 | n/a | if not b: |
---|
589 | n/a | continue |
---|
590 | n/a | next(it) |
---|
591 | n/a | d = pickle.dumps(it, proto) |
---|
592 | n/a | it = pickle.loads(d) |
---|
593 | n/a | self.assertEqual(list(it), data[1:]) |
---|
594 | n/a | |
---|
595 | n/a | def test_strip_bytearray(self): |
---|
596 | n/a | self.assertEqual(self.type2test(b'abc').strip(memoryview(b'ac')), b'b') |
---|
597 | n/a | self.assertEqual(self.type2test(b'abc').lstrip(memoryview(b'ac')), b'bc') |
---|
598 | n/a | self.assertEqual(self.type2test(b'abc').rstrip(memoryview(b'ac')), b'ab') |
---|
599 | n/a | |
---|
600 | n/a | def test_strip_string_error(self): |
---|
601 | n/a | self.assertRaises(TypeError, self.type2test(b'abc').strip, 'b') |
---|
602 | n/a | self.assertRaises(TypeError, self.type2test(b'abc').lstrip, 'b') |
---|
603 | n/a | self.assertRaises(TypeError, self.type2test(b'abc').rstrip, 'b') |
---|
604 | n/a | |
---|
605 | n/a | def test_center(self): |
---|
606 | n/a | # Fill character can be either bytes or bytearray (issue 12380) |
---|
607 | n/a | b = self.type2test(b'abc') |
---|
608 | n/a | for fill_type in (bytes, bytearray): |
---|
609 | n/a | self.assertEqual(b.center(7, fill_type(b'-')), |
---|
610 | n/a | self.type2test(b'--abc--')) |
---|
611 | n/a | |
---|
612 | n/a | def test_ljust(self): |
---|
613 | n/a | # Fill character can be either bytes or bytearray (issue 12380) |
---|
614 | n/a | b = self.type2test(b'abc') |
---|
615 | n/a | for fill_type in (bytes, bytearray): |
---|
616 | n/a | self.assertEqual(b.ljust(7, fill_type(b'-')), |
---|
617 | n/a | self.type2test(b'abc----')) |
---|
618 | n/a | |
---|
619 | n/a | def test_rjust(self): |
---|
620 | n/a | # Fill character can be either bytes or bytearray (issue 12380) |
---|
621 | n/a | b = self.type2test(b'abc') |
---|
622 | n/a | for fill_type in (bytes, bytearray): |
---|
623 | n/a | self.assertEqual(b.rjust(7, fill_type(b'-')), |
---|
624 | n/a | self.type2test(b'----abc')) |
---|
625 | n/a | |
---|
626 | n/a | def test_ord(self): |
---|
627 | n/a | b = self.type2test(b'\0A\x7f\x80\xff') |
---|
628 | n/a | self.assertEqual([ord(b[i:i+1]) for i in range(len(b))], |
---|
629 | n/a | [0, 65, 127, 128, 255]) |
---|
630 | n/a | |
---|
631 | n/a | def test_maketrans(self): |
---|
632 | n/a | 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' |
---|
633 | n/a | self.assertEqual(self.type2test.maketrans(b'abc', b'xyz'), transtable) |
---|
634 | n/a | 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' |
---|
635 | n/a | self.assertEqual(self.type2test.maketrans(b'\375\376\377', b'xyz'), transtable) |
---|
636 | n/a | self.assertRaises(ValueError, self.type2test.maketrans, b'abc', b'xyzq') |
---|
637 | n/a | self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 'def') |
---|
638 | n/a | |
---|
639 | n/a | def test_none_arguments(self): |
---|
640 | n/a | # issue 11828 |
---|
641 | n/a | b = self.type2test(b'hello') |
---|
642 | n/a | l = self.type2test(b'l') |
---|
643 | n/a | h = self.type2test(b'h') |
---|
644 | n/a | x = self.type2test(b'x') |
---|
645 | n/a | o = self.type2test(b'o') |
---|
646 | n/a | |
---|
647 | n/a | self.assertEqual(2, b.find(l, None)) |
---|
648 | n/a | self.assertEqual(3, b.find(l, -2, None)) |
---|
649 | n/a | self.assertEqual(2, b.find(l, None, -2)) |
---|
650 | n/a | self.assertEqual(0, b.find(h, None, None)) |
---|
651 | n/a | |
---|
652 | n/a | self.assertEqual(3, b.rfind(l, None)) |
---|
653 | n/a | self.assertEqual(3, b.rfind(l, -2, None)) |
---|
654 | n/a | self.assertEqual(2, b.rfind(l, None, -2)) |
---|
655 | n/a | self.assertEqual(0, b.rfind(h, None, None)) |
---|
656 | n/a | |
---|
657 | n/a | self.assertEqual(2, b.index(l, None)) |
---|
658 | n/a | self.assertEqual(3, b.index(l, -2, None)) |
---|
659 | n/a | self.assertEqual(2, b.index(l, None, -2)) |
---|
660 | n/a | self.assertEqual(0, b.index(h, None, None)) |
---|
661 | n/a | |
---|
662 | n/a | self.assertEqual(3, b.rindex(l, None)) |
---|
663 | n/a | self.assertEqual(3, b.rindex(l, -2, None)) |
---|
664 | n/a | self.assertEqual(2, b.rindex(l, None, -2)) |
---|
665 | n/a | self.assertEqual(0, b.rindex(h, None, None)) |
---|
666 | n/a | |
---|
667 | n/a | self.assertEqual(2, b.count(l, None)) |
---|
668 | n/a | self.assertEqual(1, b.count(l, -2, None)) |
---|
669 | n/a | self.assertEqual(1, b.count(l, None, -2)) |
---|
670 | n/a | self.assertEqual(0, b.count(x, None, None)) |
---|
671 | n/a | |
---|
672 | n/a | self.assertEqual(True, b.endswith(o, None)) |
---|
673 | n/a | self.assertEqual(True, b.endswith(o, -2, None)) |
---|
674 | n/a | self.assertEqual(True, b.endswith(l, None, -2)) |
---|
675 | n/a | self.assertEqual(False, b.endswith(x, None, None)) |
---|
676 | n/a | |
---|
677 | n/a | self.assertEqual(True, b.startswith(h, None)) |
---|
678 | n/a | self.assertEqual(True, b.startswith(l, -2, None)) |
---|
679 | n/a | self.assertEqual(True, b.startswith(h, None, -2)) |
---|
680 | n/a | self.assertEqual(False, b.startswith(x, None, None)) |
---|
681 | n/a | |
---|
682 | n/a | def test_integer_arguments_out_of_byte_range(self): |
---|
683 | n/a | b = self.type2test(b'hello') |
---|
684 | n/a | |
---|
685 | n/a | for method in (b.count, b.find, b.index, b.rfind, b.rindex): |
---|
686 | n/a | self.assertRaises(ValueError, method, -1) |
---|
687 | n/a | self.assertRaises(ValueError, method, 256) |
---|
688 | n/a | self.assertRaises(ValueError, method, 9999) |
---|
689 | n/a | |
---|
690 | n/a | def test_find_etc_raise_correct_error_messages(self): |
---|
691 | n/a | # issue 11828 |
---|
692 | n/a | b = self.type2test(b'hello') |
---|
693 | n/a | x = self.type2test(b'x') |
---|
694 | n/a | self.assertRaisesRegex(TypeError, r'\bfind\b', b.find, |
---|
695 | n/a | x, None, None, None) |
---|
696 | n/a | self.assertRaisesRegex(TypeError, r'\brfind\b', b.rfind, |
---|
697 | n/a | x, None, None, None) |
---|
698 | n/a | self.assertRaisesRegex(TypeError, r'\bindex\b', b.index, |
---|
699 | n/a | x, None, None, None) |
---|
700 | n/a | self.assertRaisesRegex(TypeError, r'\brindex\b', b.rindex, |
---|
701 | n/a | x, None, None, None) |
---|
702 | n/a | self.assertRaisesRegex(TypeError, r'\bcount\b', b.count, |
---|
703 | n/a | x, None, None, None) |
---|
704 | n/a | self.assertRaisesRegex(TypeError, r'\bstartswith\b', b.startswith, |
---|
705 | n/a | x, None, None, None) |
---|
706 | n/a | self.assertRaisesRegex(TypeError, r'\bendswith\b', b.endswith, |
---|
707 | n/a | x, None, None, None) |
---|
708 | n/a | |
---|
709 | n/a | def test_free_after_iterating(self): |
---|
710 | n/a | test.support.check_free_after_iterating(self, iter, self.type2test) |
---|
711 | n/a | test.support.check_free_after_iterating(self, reversed, self.type2test) |
---|
712 | n/a | |
---|
713 | n/a | def test_translate(self): |
---|
714 | n/a | b = self.type2test(b'hello') |
---|
715 | n/a | rosetta = bytearray(range(256)) |
---|
716 | n/a | rosetta[ord('o')] = ord('e') |
---|
717 | n/a | |
---|
718 | n/a | self.assertRaises(TypeError, b.translate) |
---|
719 | n/a | self.assertRaises(TypeError, b.translate, None, None) |
---|
720 | n/a | self.assertRaises(ValueError, b.translate, bytes(range(255))) |
---|
721 | n/a | |
---|
722 | n/a | c = b.translate(rosetta, b'hello') |
---|
723 | n/a | self.assertEqual(b, b'hello') |
---|
724 | n/a | self.assertIsInstance(c, self.type2test) |
---|
725 | n/a | |
---|
726 | n/a | c = b.translate(rosetta) |
---|
727 | n/a | d = b.translate(rosetta, b'') |
---|
728 | n/a | self.assertEqual(c, d) |
---|
729 | n/a | self.assertEqual(c, b'helle') |
---|
730 | n/a | |
---|
731 | n/a | c = b.translate(rosetta, b'l') |
---|
732 | n/a | self.assertEqual(c, b'hee') |
---|
733 | n/a | c = b.translate(None, b'e') |
---|
734 | n/a | self.assertEqual(c, b'hllo') |
---|
735 | n/a | |
---|
736 | n/a | # test delete as a keyword argument |
---|
737 | n/a | c = b.translate(rosetta, delete=b'') |
---|
738 | n/a | self.assertEqual(c, b'helle') |
---|
739 | n/a | c = b.translate(rosetta, delete=b'l') |
---|
740 | n/a | self.assertEqual(c, b'hee') |
---|
741 | n/a | c = b.translate(None, delete=b'e') |
---|
742 | n/a | self.assertEqual(c, b'hllo') |
---|
743 | n/a | |
---|
744 | n/a | |
---|
745 | n/a | class BytesTest(BaseBytesTest, unittest.TestCase): |
---|
746 | n/a | type2test = bytes |
---|
747 | n/a | |
---|
748 | n/a | def test_getitem_error(self): |
---|
749 | n/a | msg = "byte indices must be integers or slices" |
---|
750 | n/a | with self.assertRaisesRegex(TypeError, msg): |
---|
751 | n/a | b'python'['a'] |
---|
752 | n/a | |
---|
753 | n/a | def test_buffer_is_readonly(self): |
---|
754 | n/a | fd = os.open(__file__, os.O_RDONLY) |
---|
755 | n/a | with open(fd, "rb", buffering=0) as f: |
---|
756 | n/a | self.assertRaises(TypeError, f.readinto, b"") |
---|
757 | n/a | |
---|
758 | n/a | def test_custom(self): |
---|
759 | n/a | class A: |
---|
760 | n/a | def __bytes__(self): |
---|
761 | n/a | return b'abc' |
---|
762 | n/a | self.assertEqual(bytes(A()), b'abc') |
---|
763 | n/a | class A: pass |
---|
764 | n/a | self.assertRaises(TypeError, bytes, A()) |
---|
765 | n/a | class A: |
---|
766 | n/a | def __bytes__(self): |
---|
767 | n/a | return None |
---|
768 | n/a | self.assertRaises(TypeError, bytes, A()) |
---|
769 | n/a | class A: |
---|
770 | n/a | def __bytes__(self): |
---|
771 | n/a | return b'a' |
---|
772 | n/a | def __index__(self): |
---|
773 | n/a | return 42 |
---|
774 | n/a | self.assertEqual(bytes(A()), b'a') |
---|
775 | n/a | # Issue #25766 |
---|
776 | n/a | class A(str): |
---|
777 | n/a | def __bytes__(self): |
---|
778 | n/a | return b'abc' |
---|
779 | n/a | self.assertEqual(bytes(A('\u20ac')), b'abc') |
---|
780 | n/a | self.assertEqual(bytes(A('\u20ac'), 'iso8859-15'), b'\xa4') |
---|
781 | n/a | # Issue #24731 |
---|
782 | n/a | class A: |
---|
783 | n/a | def __bytes__(self): |
---|
784 | n/a | return OtherBytesSubclass(b'abc') |
---|
785 | n/a | self.assertEqual(bytes(A()), b'abc') |
---|
786 | n/a | self.assertIs(type(bytes(A())), OtherBytesSubclass) |
---|
787 | n/a | self.assertEqual(BytesSubclass(A()), b'abc') |
---|
788 | n/a | self.assertIs(type(BytesSubclass(A())), BytesSubclass) |
---|
789 | n/a | |
---|
790 | n/a | # Test PyBytes_FromFormat() |
---|
791 | n/a | def test_from_format(self): |
---|
792 | n/a | ctypes = test.support.import_module('ctypes') |
---|
793 | n/a | _testcapi = test.support.import_module('_testcapi') |
---|
794 | n/a | from ctypes import pythonapi, py_object |
---|
795 | n/a | from ctypes import ( |
---|
796 | n/a | c_int, c_uint, |
---|
797 | n/a | c_long, c_ulong, |
---|
798 | n/a | c_size_t, c_ssize_t, |
---|
799 | n/a | c_char_p) |
---|
800 | n/a | |
---|
801 | n/a | PyBytes_FromFormat = pythonapi.PyBytes_FromFormat |
---|
802 | n/a | PyBytes_FromFormat.restype = py_object |
---|
803 | n/a | |
---|
804 | n/a | # basic tests |
---|
805 | n/a | self.assertEqual(PyBytes_FromFormat(b'format'), |
---|
806 | n/a | b'format') |
---|
807 | n/a | self.assertEqual(PyBytes_FromFormat(b'Hello %s !', b'world'), |
---|
808 | n/a | b'Hello world !') |
---|
809 | n/a | |
---|
810 | n/a | # test formatters |
---|
811 | n/a | self.assertEqual(PyBytes_FromFormat(b'c=%c', c_int(0)), |
---|
812 | n/a | b'c=\0') |
---|
813 | n/a | self.assertEqual(PyBytes_FromFormat(b'c=%c', c_int(ord('@'))), |
---|
814 | n/a | b'c=@') |
---|
815 | n/a | self.assertEqual(PyBytes_FromFormat(b'c=%c', c_int(255)), |
---|
816 | n/a | b'c=\xff') |
---|
817 | n/a | self.assertEqual(PyBytes_FromFormat(b'd=%d ld=%ld zd=%zd', |
---|
818 | n/a | c_int(1), c_long(2), |
---|
819 | n/a | c_size_t(3)), |
---|
820 | n/a | b'd=1 ld=2 zd=3') |
---|
821 | n/a | self.assertEqual(PyBytes_FromFormat(b'd=%d ld=%ld zd=%zd', |
---|
822 | n/a | c_int(-1), c_long(-2), |
---|
823 | n/a | c_size_t(-3)), |
---|
824 | n/a | b'd=-1 ld=-2 zd=-3') |
---|
825 | n/a | self.assertEqual(PyBytes_FromFormat(b'u=%u lu=%lu zu=%zu', |
---|
826 | n/a | c_uint(123), c_ulong(456), |
---|
827 | n/a | c_size_t(789)), |
---|
828 | n/a | b'u=123 lu=456 zu=789') |
---|
829 | n/a | self.assertEqual(PyBytes_FromFormat(b'i=%i', c_int(123)), |
---|
830 | n/a | b'i=123') |
---|
831 | n/a | self.assertEqual(PyBytes_FromFormat(b'i=%i', c_int(-123)), |
---|
832 | n/a | b'i=-123') |
---|
833 | n/a | self.assertEqual(PyBytes_FromFormat(b'x=%x', c_int(0xabc)), |
---|
834 | n/a | b'x=abc') |
---|
835 | n/a | |
---|
836 | n/a | sizeof_ptr = ctypes.sizeof(c_char_p) |
---|
837 | n/a | |
---|
838 | n/a | if os.name == 'nt': |
---|
839 | n/a | # Windows (MSCRT) |
---|
840 | n/a | ptr_format = '0x%0{}X'.format(2 * sizeof_ptr) |
---|
841 | n/a | def ptr_formatter(ptr): |
---|
842 | n/a | return (ptr_format % ptr) |
---|
843 | n/a | else: |
---|
844 | n/a | # UNIX (glibc) |
---|
845 | n/a | def ptr_formatter(ptr): |
---|
846 | n/a | return '%#x' % ptr |
---|
847 | n/a | |
---|
848 | n/a | ptr = 0xabcdef |
---|
849 | n/a | self.assertEqual(PyBytes_FromFormat(b'ptr=%p', c_char_p(ptr)), |
---|
850 | n/a | ('ptr=' + ptr_formatter(ptr)).encode('ascii')) |
---|
851 | n/a | self.assertEqual(PyBytes_FromFormat(b's=%s', c_char_p(b'cstr')), |
---|
852 | n/a | b's=cstr') |
---|
853 | n/a | |
---|
854 | n/a | # test minimum and maximum integer values |
---|
855 | n/a | size_max = c_size_t(-1).value |
---|
856 | n/a | for formatstr, ctypes_type, value, py_formatter in ( |
---|
857 | n/a | (b'%d', c_int, _testcapi.INT_MIN, str), |
---|
858 | n/a | (b'%d', c_int, _testcapi.INT_MAX, str), |
---|
859 | n/a | (b'%ld', c_long, _testcapi.LONG_MIN, str), |
---|
860 | n/a | (b'%ld', c_long, _testcapi.LONG_MAX, str), |
---|
861 | n/a | (b'%lu', c_ulong, _testcapi.ULONG_MAX, str), |
---|
862 | n/a | (b'%zd', c_ssize_t, _testcapi.PY_SSIZE_T_MIN, str), |
---|
863 | n/a | (b'%zd', c_ssize_t, _testcapi.PY_SSIZE_T_MAX, str), |
---|
864 | n/a | (b'%zu', c_size_t, size_max, str), |
---|
865 | n/a | (b'%p', c_char_p, size_max, ptr_formatter), |
---|
866 | n/a | ): |
---|
867 | n/a | self.assertEqual(PyBytes_FromFormat(formatstr, ctypes_type(value)), |
---|
868 | n/a | py_formatter(value).encode('ascii')), |
---|
869 | n/a | |
---|
870 | n/a | # width and precision (width is currently ignored) |
---|
871 | n/a | self.assertEqual(PyBytes_FromFormat(b'%5s', b'a'), |
---|
872 | n/a | b'a') |
---|
873 | n/a | self.assertEqual(PyBytes_FromFormat(b'%.3s', b'abcdef'), |
---|
874 | n/a | b'abc') |
---|
875 | n/a | |
---|
876 | n/a | # '%%' formatter |
---|
877 | n/a | self.assertEqual(PyBytes_FromFormat(b'%%'), |
---|
878 | n/a | b'%') |
---|
879 | n/a | self.assertEqual(PyBytes_FromFormat(b'[%%]'), |
---|
880 | n/a | b'[%]') |
---|
881 | n/a | self.assertEqual(PyBytes_FromFormat(b'%%%c', c_int(ord('_'))), |
---|
882 | n/a | b'%_') |
---|
883 | n/a | self.assertEqual(PyBytes_FromFormat(b'%%s'), |
---|
884 | n/a | b'%s') |
---|
885 | n/a | |
---|
886 | n/a | # Invalid formats and partial formatting |
---|
887 | n/a | self.assertEqual(PyBytes_FromFormat(b'%'), b'%') |
---|
888 | n/a | self.assertEqual(PyBytes_FromFormat(b'x=%i y=%', c_int(2), c_int(3)), |
---|
889 | n/a | b'x=2 y=%') |
---|
890 | n/a | |
---|
891 | n/a | # Issue #19969: %c must raise OverflowError for values |
---|
892 | n/a | # not in the range [0; 255] |
---|
893 | n/a | self.assertRaises(OverflowError, |
---|
894 | n/a | PyBytes_FromFormat, b'%c', c_int(-1)) |
---|
895 | n/a | self.assertRaises(OverflowError, |
---|
896 | n/a | PyBytes_FromFormat, b'%c', c_int(256)) |
---|
897 | n/a | |
---|
898 | n/a | def test_bytes_blocking(self): |
---|
899 | n/a | class IterationBlocked(list): |
---|
900 | n/a | __bytes__ = None |
---|
901 | n/a | i = [0, 1, 2, 3] |
---|
902 | n/a | self.assertEqual(bytes(i), b'\x00\x01\x02\x03') |
---|
903 | n/a | self.assertRaises(TypeError, bytes, IterationBlocked(i)) |
---|
904 | n/a | |
---|
905 | n/a | # At least in CPython, because bytes.__new__ and the C API |
---|
906 | n/a | # PyBytes_FromObject have different fallback rules, integer |
---|
907 | n/a | # fallback is handled specially, so test separately. |
---|
908 | n/a | class IntBlocked(int): |
---|
909 | n/a | __bytes__ = None |
---|
910 | n/a | self.assertEqual(bytes(3), b'\0\0\0') |
---|
911 | n/a | self.assertRaises(TypeError, bytes, IntBlocked(3)) |
---|
912 | n/a | |
---|
913 | n/a | # While there is no separately-defined rule for handling bytes |
---|
914 | n/a | # subclasses differently from other buffer-interface classes, |
---|
915 | n/a | # an implementation may well special-case them (as CPython 2.x |
---|
916 | n/a | # str did), so test them separately. |
---|
917 | n/a | class BytesSubclassBlocked(bytes): |
---|
918 | n/a | __bytes__ = None |
---|
919 | n/a | self.assertEqual(bytes(b'ab'), b'ab') |
---|
920 | n/a | self.assertRaises(TypeError, bytes, BytesSubclassBlocked(b'ab')) |
---|
921 | n/a | |
---|
922 | n/a | class BufferBlocked(bytearray): |
---|
923 | n/a | __bytes__ = None |
---|
924 | n/a | ba, bb = bytearray(b'ab'), BufferBlocked(b'ab') |
---|
925 | n/a | self.assertEqual(bytes(ba), b'ab') |
---|
926 | n/a | self.assertRaises(TypeError, bytes, bb) |
---|
927 | n/a | |
---|
928 | n/a | |
---|
929 | n/a | class ByteArrayTest(BaseBytesTest, unittest.TestCase): |
---|
930 | n/a | type2test = bytearray |
---|
931 | n/a | |
---|
932 | n/a | def test_getitem_error(self): |
---|
933 | n/a | msg = "bytearray indices must be integers or slices" |
---|
934 | n/a | with self.assertRaisesRegex(TypeError, msg): |
---|
935 | n/a | bytearray(b'python')['a'] |
---|
936 | n/a | |
---|
937 | n/a | def test_setitem_error(self): |
---|
938 | n/a | msg = "bytearray indices must be integers or slices" |
---|
939 | n/a | with self.assertRaisesRegex(TypeError, msg): |
---|
940 | n/a | b = bytearray(b'python') |
---|
941 | n/a | b['a'] = "python" |
---|
942 | n/a | |
---|
943 | n/a | def test_nohash(self): |
---|
944 | n/a | self.assertRaises(TypeError, hash, bytearray()) |
---|
945 | n/a | |
---|
946 | n/a | def test_bytearray_api(self): |
---|
947 | n/a | short_sample = b"Hello world\n" |
---|
948 | n/a | sample = short_sample + b"\0"*(20 - len(short_sample)) |
---|
949 | n/a | tfn = tempfile.mktemp() |
---|
950 | n/a | try: |
---|
951 | n/a | # Prepare |
---|
952 | n/a | with open(tfn, "wb") as f: |
---|
953 | n/a | f.write(short_sample) |
---|
954 | n/a | # Test readinto |
---|
955 | n/a | with open(tfn, "rb") as f: |
---|
956 | n/a | b = bytearray(20) |
---|
957 | n/a | n = f.readinto(b) |
---|
958 | n/a | self.assertEqual(n, len(short_sample)) |
---|
959 | n/a | self.assertEqual(list(b), list(sample)) |
---|
960 | n/a | # Test writing in binary mode |
---|
961 | n/a | with open(tfn, "wb") as f: |
---|
962 | n/a | f.write(b) |
---|
963 | n/a | with open(tfn, "rb") as f: |
---|
964 | n/a | self.assertEqual(f.read(), sample) |
---|
965 | n/a | # Text mode is ambiguous; don't test |
---|
966 | n/a | finally: |
---|
967 | n/a | try: |
---|
968 | n/a | os.remove(tfn) |
---|
969 | n/a | except OSError: |
---|
970 | n/a | pass |
---|
971 | n/a | |
---|
972 | n/a | def test_reverse(self): |
---|
973 | n/a | b = bytearray(b'hello') |
---|
974 | n/a | self.assertEqual(b.reverse(), None) |
---|
975 | n/a | self.assertEqual(b, b'olleh') |
---|
976 | n/a | b = bytearray(b'hello1') # test even number of items |
---|
977 | n/a | b.reverse() |
---|
978 | n/a | self.assertEqual(b, b'1olleh') |
---|
979 | n/a | b = bytearray() |
---|
980 | n/a | b.reverse() |
---|
981 | n/a | self.assertFalse(b) |
---|
982 | n/a | |
---|
983 | n/a | def test_clear(self): |
---|
984 | n/a | b = bytearray(b'python') |
---|
985 | n/a | b.clear() |
---|
986 | n/a | self.assertEqual(b, b'') |
---|
987 | n/a | |
---|
988 | n/a | b = bytearray(b'') |
---|
989 | n/a | b.clear() |
---|
990 | n/a | self.assertEqual(b, b'') |
---|
991 | n/a | |
---|
992 | n/a | b = bytearray(b'') |
---|
993 | n/a | b.append(ord('r')) |
---|
994 | n/a | b.clear() |
---|
995 | n/a | b.append(ord('p')) |
---|
996 | n/a | self.assertEqual(b, b'p') |
---|
997 | n/a | |
---|
998 | n/a | def test_copy(self): |
---|
999 | n/a | b = bytearray(b'abc') |
---|
1000 | n/a | bb = b.copy() |
---|
1001 | n/a | self.assertEqual(bb, b'abc') |
---|
1002 | n/a | |
---|
1003 | n/a | b = bytearray(b'') |
---|
1004 | n/a | bb = b.copy() |
---|
1005 | n/a | self.assertEqual(bb, b'') |
---|
1006 | n/a | |
---|
1007 | n/a | # test that it's indeed a copy and not a reference |
---|
1008 | n/a | b = bytearray(b'abc') |
---|
1009 | n/a | bb = b.copy() |
---|
1010 | n/a | self.assertEqual(b, bb) |
---|
1011 | n/a | self.assertIsNot(b, bb) |
---|
1012 | n/a | bb.append(ord('d')) |
---|
1013 | n/a | self.assertEqual(bb, b'abcd') |
---|
1014 | n/a | self.assertEqual(b, b'abc') |
---|
1015 | n/a | |
---|
1016 | n/a | def test_regexps(self): |
---|
1017 | n/a | def by(s): |
---|
1018 | n/a | return bytearray(map(ord, s)) |
---|
1019 | n/a | b = by("Hello, world") |
---|
1020 | n/a | self.assertEqual(re.findall(br"\w+", b), [by("Hello"), by("world")]) |
---|
1021 | n/a | |
---|
1022 | n/a | def test_setitem(self): |
---|
1023 | n/a | b = bytearray([1, 2, 3]) |
---|
1024 | n/a | b[1] = 100 |
---|
1025 | n/a | self.assertEqual(b, bytearray([1, 100, 3])) |
---|
1026 | n/a | b[-1] = 200 |
---|
1027 | n/a | self.assertEqual(b, bytearray([1, 100, 200])) |
---|
1028 | n/a | b[0] = Indexable(10) |
---|
1029 | n/a | self.assertEqual(b, bytearray([10, 100, 200])) |
---|
1030 | n/a | try: |
---|
1031 | n/a | b[3] = 0 |
---|
1032 | n/a | self.fail("Didn't raise IndexError") |
---|
1033 | n/a | except IndexError: |
---|
1034 | n/a | pass |
---|
1035 | n/a | try: |
---|
1036 | n/a | b[-10] = 0 |
---|
1037 | n/a | self.fail("Didn't raise IndexError") |
---|
1038 | n/a | except IndexError: |
---|
1039 | n/a | pass |
---|
1040 | n/a | try: |
---|
1041 | n/a | b[0] = 256 |
---|
1042 | n/a | self.fail("Didn't raise ValueError") |
---|
1043 | n/a | except ValueError: |
---|
1044 | n/a | pass |
---|
1045 | n/a | try: |
---|
1046 | n/a | b[0] = Indexable(-1) |
---|
1047 | n/a | self.fail("Didn't raise ValueError") |
---|
1048 | n/a | except ValueError: |
---|
1049 | n/a | pass |
---|
1050 | n/a | try: |
---|
1051 | n/a | b[0] = None |
---|
1052 | n/a | self.fail("Didn't raise TypeError") |
---|
1053 | n/a | except TypeError: |
---|
1054 | n/a | pass |
---|
1055 | n/a | |
---|
1056 | n/a | def test_delitem(self): |
---|
1057 | n/a | b = bytearray(range(10)) |
---|
1058 | n/a | del b[0] |
---|
1059 | n/a | self.assertEqual(b, bytearray(range(1, 10))) |
---|
1060 | n/a | del b[-1] |
---|
1061 | n/a | self.assertEqual(b, bytearray(range(1, 9))) |
---|
1062 | n/a | del b[4] |
---|
1063 | n/a | self.assertEqual(b, bytearray([1, 2, 3, 4, 6, 7, 8])) |
---|
1064 | n/a | |
---|
1065 | n/a | def test_setslice(self): |
---|
1066 | n/a | b = bytearray(range(10)) |
---|
1067 | n/a | self.assertEqual(list(b), list(range(10))) |
---|
1068 | n/a | |
---|
1069 | n/a | b[0:5] = bytearray([1, 1, 1, 1, 1]) |
---|
1070 | n/a | self.assertEqual(b, bytearray([1, 1, 1, 1, 1, 5, 6, 7, 8, 9])) |
---|
1071 | n/a | |
---|
1072 | n/a | del b[0:-5] |
---|
1073 | n/a | self.assertEqual(b, bytearray([5, 6, 7, 8, 9])) |
---|
1074 | n/a | |
---|
1075 | n/a | b[0:0] = bytearray([0, 1, 2, 3, 4]) |
---|
1076 | n/a | self.assertEqual(b, bytearray(range(10))) |
---|
1077 | n/a | |
---|
1078 | n/a | b[-7:-3] = bytearray([100, 101]) |
---|
1079 | n/a | self.assertEqual(b, bytearray([0, 1, 2, 100, 101, 7, 8, 9])) |
---|
1080 | n/a | |
---|
1081 | n/a | b[3:5] = [3, 4, 5, 6] |
---|
1082 | n/a | self.assertEqual(b, bytearray(range(10))) |
---|
1083 | n/a | |
---|
1084 | n/a | b[3:0] = [42, 42, 42] |
---|
1085 | n/a | self.assertEqual(b, bytearray([0, 1, 2, 42, 42, 42, 3, 4, 5, 6, 7, 8, 9])) |
---|
1086 | n/a | |
---|
1087 | n/a | b[3:] = b'foo' |
---|
1088 | n/a | self.assertEqual(b, bytearray([0, 1, 2, 102, 111, 111])) |
---|
1089 | n/a | |
---|
1090 | n/a | b[:3] = memoryview(b'foo') |
---|
1091 | n/a | self.assertEqual(b, bytearray([102, 111, 111, 102, 111, 111])) |
---|
1092 | n/a | |
---|
1093 | n/a | b[3:4] = [] |
---|
1094 | n/a | self.assertEqual(b, bytearray([102, 111, 111, 111, 111])) |
---|
1095 | n/a | |
---|
1096 | n/a | for elem in [5, -5, 0, int(10e20), 'str', 2.3, |
---|
1097 | n/a | ['a', 'b'], [b'a', b'b'], [[]]]: |
---|
1098 | n/a | with self.assertRaises(TypeError): |
---|
1099 | n/a | b[3:4] = elem |
---|
1100 | n/a | |
---|
1101 | n/a | for elem in [[254, 255, 256], [-256, 9000]]: |
---|
1102 | n/a | with self.assertRaises(ValueError): |
---|
1103 | n/a | b[3:4] = elem |
---|
1104 | n/a | |
---|
1105 | n/a | def test_setslice_extend(self): |
---|
1106 | n/a | # Exercise the resizing logic (see issue #19087) |
---|
1107 | n/a | b = bytearray(range(100)) |
---|
1108 | n/a | self.assertEqual(list(b), list(range(100))) |
---|
1109 | n/a | del b[:10] |
---|
1110 | n/a | self.assertEqual(list(b), list(range(10, 100))) |
---|
1111 | n/a | b.extend(range(100, 110)) |
---|
1112 | n/a | self.assertEqual(list(b), list(range(10, 110))) |
---|
1113 | n/a | |
---|
1114 | n/a | def test_fifo_overrun(self): |
---|
1115 | n/a | # Test for issue #23985, a buffer overrun when implementing a FIFO |
---|
1116 | n/a | # Build Python in pydebug mode for best results. |
---|
1117 | n/a | b = bytearray(10) |
---|
1118 | n/a | b.pop() # Defeat expanding buffer off-by-one quirk |
---|
1119 | n/a | del b[:1] # Advance start pointer without reallocating |
---|
1120 | n/a | b += bytes(2) # Append exactly the number of deleted bytes |
---|
1121 | n/a | del b # Free memory buffer, allowing pydebug verification |
---|
1122 | n/a | |
---|
1123 | n/a | def test_del_expand(self): |
---|
1124 | n/a | # Reducing the size should not expand the buffer (issue #23985) |
---|
1125 | n/a | b = bytearray(10) |
---|
1126 | n/a | size = sys.getsizeof(b) |
---|
1127 | n/a | del b[:1] |
---|
1128 | n/a | self.assertLessEqual(sys.getsizeof(b), size) |
---|
1129 | n/a | |
---|
1130 | n/a | def test_extended_set_del_slice(self): |
---|
1131 | n/a | indices = (0, None, 1, 3, 19, 300, 1<<333, -1, -2, -31, -300) |
---|
1132 | n/a | for start in indices: |
---|
1133 | n/a | for stop in indices: |
---|
1134 | n/a | # Skip invalid step 0 |
---|
1135 | n/a | for step in indices[1:]: |
---|
1136 | n/a | L = list(range(255)) |
---|
1137 | n/a | b = bytearray(L) |
---|
1138 | n/a | # Make sure we have a slice of exactly the right length, |
---|
1139 | n/a | # but with different data. |
---|
1140 | n/a | data = L[start:stop:step] |
---|
1141 | n/a | data.reverse() |
---|
1142 | n/a | L[start:stop:step] = data |
---|
1143 | n/a | b[start:stop:step] = data |
---|
1144 | n/a | self.assertEqual(b, bytearray(L)) |
---|
1145 | n/a | |
---|
1146 | n/a | del L[start:stop:step] |
---|
1147 | n/a | del b[start:stop:step] |
---|
1148 | n/a | self.assertEqual(b, bytearray(L)) |
---|
1149 | n/a | |
---|
1150 | n/a | def test_setslice_trap(self): |
---|
1151 | n/a | # This test verifies that we correctly handle assigning self |
---|
1152 | n/a | # to a slice of self (the old Lambert Meertens trap). |
---|
1153 | n/a | b = bytearray(range(256)) |
---|
1154 | n/a | b[8:] = b |
---|
1155 | n/a | self.assertEqual(b, bytearray(list(range(8)) + list(range(256)))) |
---|
1156 | n/a | |
---|
1157 | n/a | def test_iconcat(self): |
---|
1158 | n/a | b = bytearray(b"abc") |
---|
1159 | n/a | b1 = b |
---|
1160 | n/a | b += b"def" |
---|
1161 | n/a | self.assertEqual(b, b"abcdef") |
---|
1162 | n/a | self.assertEqual(b, b1) |
---|
1163 | n/a | self.assertTrue(b is b1) |
---|
1164 | n/a | b += b"xyz" |
---|
1165 | n/a | self.assertEqual(b, b"abcdefxyz") |
---|
1166 | n/a | try: |
---|
1167 | n/a | b += "" |
---|
1168 | n/a | except TypeError: |
---|
1169 | n/a | pass |
---|
1170 | n/a | else: |
---|
1171 | n/a | self.fail("bytes += unicode didn't raise TypeError") |
---|
1172 | n/a | |
---|
1173 | n/a | def test_irepeat(self): |
---|
1174 | n/a | b = bytearray(b"abc") |
---|
1175 | n/a | b1 = b |
---|
1176 | n/a | b *= 3 |
---|
1177 | n/a | self.assertEqual(b, b"abcabcabc") |
---|
1178 | n/a | self.assertEqual(b, b1) |
---|
1179 | n/a | self.assertTrue(b is b1) |
---|
1180 | n/a | |
---|
1181 | n/a | def test_irepeat_1char(self): |
---|
1182 | n/a | b = bytearray(b"x") |
---|
1183 | n/a | b1 = b |
---|
1184 | n/a | b *= 100 |
---|
1185 | n/a | self.assertEqual(b, b"x"*100) |
---|
1186 | n/a | self.assertEqual(b, b1) |
---|
1187 | n/a | self.assertTrue(b is b1) |
---|
1188 | n/a | |
---|
1189 | n/a | def test_alloc(self): |
---|
1190 | n/a | b = bytearray() |
---|
1191 | n/a | alloc = b.__alloc__() |
---|
1192 | n/a | self.assertTrue(alloc >= 0) |
---|
1193 | n/a | seq = [alloc] |
---|
1194 | n/a | for i in range(100): |
---|
1195 | n/a | b += b"x" |
---|
1196 | n/a | alloc = b.__alloc__() |
---|
1197 | n/a | self.assertGreater(alloc, len(b)) # including trailing null byte |
---|
1198 | n/a | if alloc not in seq: |
---|
1199 | n/a | seq.append(alloc) |
---|
1200 | n/a | |
---|
1201 | n/a | def test_init_alloc(self): |
---|
1202 | n/a | b = bytearray() |
---|
1203 | n/a | def g(): |
---|
1204 | n/a | for i in range(1, 100): |
---|
1205 | n/a | yield i |
---|
1206 | n/a | a = list(b) |
---|
1207 | n/a | self.assertEqual(a, list(range(1, len(a)+1))) |
---|
1208 | n/a | self.assertEqual(len(b), len(a)) |
---|
1209 | n/a | self.assertLessEqual(len(b), i) |
---|
1210 | n/a | alloc = b.__alloc__() |
---|
1211 | n/a | self.assertGreater(alloc, len(b)) # including trailing null byte |
---|
1212 | n/a | b.__init__(g()) |
---|
1213 | n/a | self.assertEqual(list(b), list(range(1, 100))) |
---|
1214 | n/a | self.assertEqual(len(b), 99) |
---|
1215 | n/a | alloc = b.__alloc__() |
---|
1216 | n/a | self.assertGreater(alloc, len(b)) |
---|
1217 | n/a | |
---|
1218 | n/a | def test_extend(self): |
---|
1219 | n/a | orig = b'hello' |
---|
1220 | n/a | a = bytearray(orig) |
---|
1221 | n/a | a.extend(a) |
---|
1222 | n/a | self.assertEqual(a, orig + orig) |
---|
1223 | n/a | self.assertEqual(a[5:], orig) |
---|
1224 | n/a | a = bytearray(b'') |
---|
1225 | n/a | # Test iterators that don't have a __length_hint__ |
---|
1226 | n/a | a.extend(map(int, orig * 25)) |
---|
1227 | n/a | a.extend(int(x) for x in orig * 25) |
---|
1228 | n/a | self.assertEqual(a, orig * 50) |
---|
1229 | n/a | self.assertEqual(a[-5:], orig) |
---|
1230 | n/a | a = bytearray(b'') |
---|
1231 | n/a | a.extend(iter(map(int, orig * 50))) |
---|
1232 | n/a | self.assertEqual(a, orig * 50) |
---|
1233 | n/a | self.assertEqual(a[-5:], orig) |
---|
1234 | n/a | a = bytearray(b'') |
---|
1235 | n/a | a.extend(list(map(int, orig * 50))) |
---|
1236 | n/a | self.assertEqual(a, orig * 50) |
---|
1237 | n/a | self.assertEqual(a[-5:], orig) |
---|
1238 | n/a | a = bytearray(b'') |
---|
1239 | n/a | self.assertRaises(ValueError, a.extend, [0, 1, 2, 256]) |
---|
1240 | n/a | self.assertRaises(ValueError, a.extend, [0, 1, 2, -1]) |
---|
1241 | n/a | self.assertEqual(len(a), 0) |
---|
1242 | n/a | a = bytearray(b'') |
---|
1243 | n/a | a.extend([Indexable(ord('a'))]) |
---|
1244 | n/a | self.assertEqual(a, b'a') |
---|
1245 | n/a | |
---|
1246 | n/a | def test_remove(self): |
---|
1247 | n/a | b = bytearray(b'hello') |
---|
1248 | n/a | b.remove(ord('l')) |
---|
1249 | n/a | self.assertEqual(b, b'helo') |
---|
1250 | n/a | b.remove(ord('l')) |
---|
1251 | n/a | self.assertEqual(b, b'heo') |
---|
1252 | n/a | self.assertRaises(ValueError, lambda: b.remove(ord('l'))) |
---|
1253 | n/a | self.assertRaises(ValueError, lambda: b.remove(400)) |
---|
1254 | n/a | self.assertRaises(TypeError, lambda: b.remove('e')) |
---|
1255 | n/a | # remove first and last |
---|
1256 | n/a | b.remove(ord('o')) |
---|
1257 | n/a | b.remove(ord('h')) |
---|
1258 | n/a | self.assertEqual(b, b'e') |
---|
1259 | n/a | self.assertRaises(TypeError, lambda: b.remove(b'e')) |
---|
1260 | n/a | b.remove(Indexable(ord('e'))) |
---|
1261 | n/a | self.assertEqual(b, b'') |
---|
1262 | n/a | |
---|
1263 | n/a | # test values outside of the ascii range: (0, 127) |
---|
1264 | n/a | c = bytearray([126, 127, 128, 129]) |
---|
1265 | n/a | c.remove(127) |
---|
1266 | n/a | self.assertEqual(c, bytes([126, 128, 129])) |
---|
1267 | n/a | c.remove(129) |
---|
1268 | n/a | self.assertEqual(c, bytes([126, 128])) |
---|
1269 | n/a | |
---|
1270 | n/a | def test_pop(self): |
---|
1271 | n/a | b = bytearray(b'world') |
---|
1272 | n/a | self.assertEqual(b.pop(), ord('d')) |
---|
1273 | n/a | self.assertEqual(b.pop(0), ord('w')) |
---|
1274 | n/a | self.assertEqual(b.pop(-2), ord('r')) |
---|
1275 | n/a | self.assertRaises(IndexError, lambda: b.pop(10)) |
---|
1276 | n/a | self.assertRaises(IndexError, lambda: bytearray().pop()) |
---|
1277 | n/a | # test for issue #6846 |
---|
1278 | n/a | self.assertEqual(bytearray(b'\xff').pop(), 0xff) |
---|
1279 | n/a | |
---|
1280 | n/a | def test_nosort(self): |
---|
1281 | n/a | self.assertRaises(AttributeError, lambda: bytearray().sort()) |
---|
1282 | n/a | |
---|
1283 | n/a | def test_append(self): |
---|
1284 | n/a | b = bytearray(b'hell') |
---|
1285 | n/a | b.append(ord('o')) |
---|
1286 | n/a | self.assertEqual(b, b'hello') |
---|
1287 | n/a | self.assertEqual(b.append(100), None) |
---|
1288 | n/a | b = bytearray() |
---|
1289 | n/a | b.append(ord('A')) |
---|
1290 | n/a | self.assertEqual(len(b), 1) |
---|
1291 | n/a | self.assertRaises(TypeError, lambda: b.append(b'o')) |
---|
1292 | n/a | b = bytearray() |
---|
1293 | n/a | b.append(Indexable(ord('A'))) |
---|
1294 | n/a | self.assertEqual(b, b'A') |
---|
1295 | n/a | |
---|
1296 | n/a | def test_insert(self): |
---|
1297 | n/a | b = bytearray(b'msssspp') |
---|
1298 | n/a | b.insert(1, ord('i')) |
---|
1299 | n/a | b.insert(4, ord('i')) |
---|
1300 | n/a | b.insert(-2, ord('i')) |
---|
1301 | n/a | b.insert(1000, ord('i')) |
---|
1302 | n/a | self.assertEqual(b, b'mississippi') |
---|
1303 | n/a | self.assertRaises(TypeError, lambda: b.insert(0, b'1')) |
---|
1304 | n/a | b = bytearray() |
---|
1305 | n/a | b.insert(0, Indexable(ord('A'))) |
---|
1306 | n/a | self.assertEqual(b, b'A') |
---|
1307 | n/a | |
---|
1308 | n/a | def test_copied(self): |
---|
1309 | n/a | # Issue 4348. Make sure that operations that don't mutate the array |
---|
1310 | n/a | # copy the bytes. |
---|
1311 | n/a | b = bytearray(b'abc') |
---|
1312 | n/a | self.assertFalse(b is b.replace(b'abc', b'cde', 0)) |
---|
1313 | n/a | |
---|
1314 | n/a | t = bytearray([i for i in range(256)]) |
---|
1315 | n/a | x = bytearray(b'') |
---|
1316 | n/a | self.assertFalse(x is x.translate(t)) |
---|
1317 | n/a | |
---|
1318 | n/a | def test_partition_bytearray_doesnt_share_nullstring(self): |
---|
1319 | n/a | a, b, c = bytearray(b"x").partition(b"y") |
---|
1320 | n/a | self.assertEqual(b, b"") |
---|
1321 | n/a | self.assertEqual(c, b"") |
---|
1322 | n/a | self.assertTrue(b is not c) |
---|
1323 | n/a | b += b"!" |
---|
1324 | n/a | self.assertEqual(c, b"") |
---|
1325 | n/a | a, b, c = bytearray(b"x").partition(b"y") |
---|
1326 | n/a | self.assertEqual(b, b"") |
---|
1327 | n/a | self.assertEqual(c, b"") |
---|
1328 | n/a | # Same for rpartition |
---|
1329 | n/a | b, c, a = bytearray(b"x").rpartition(b"y") |
---|
1330 | n/a | self.assertEqual(b, b"") |
---|
1331 | n/a | self.assertEqual(c, b"") |
---|
1332 | n/a | self.assertTrue(b is not c) |
---|
1333 | n/a | b += b"!" |
---|
1334 | n/a | self.assertEqual(c, b"") |
---|
1335 | n/a | c, b, a = bytearray(b"x").rpartition(b"y") |
---|
1336 | n/a | self.assertEqual(b, b"") |
---|
1337 | n/a | self.assertEqual(c, b"") |
---|
1338 | n/a | |
---|
1339 | n/a | def test_resize_forbidden(self): |
---|
1340 | n/a | # #4509: can't resize a bytearray when there are buffer exports, even |
---|
1341 | n/a | # if it wouldn't reallocate the underlying buffer. |
---|
1342 | n/a | # Furthermore, no destructive changes to the buffer may be applied |
---|
1343 | n/a | # before raising the error. |
---|
1344 | n/a | b = bytearray(range(10)) |
---|
1345 | n/a | v = memoryview(b) |
---|
1346 | n/a | def resize(n): |
---|
1347 | n/a | b[1:-1] = range(n + 1, 2*n - 1) |
---|
1348 | n/a | resize(10) |
---|
1349 | n/a | orig = b[:] |
---|
1350 | n/a | self.assertRaises(BufferError, resize, 11) |
---|
1351 | n/a | self.assertEqual(b, orig) |
---|
1352 | n/a | self.assertRaises(BufferError, resize, 9) |
---|
1353 | n/a | self.assertEqual(b, orig) |
---|
1354 | n/a | self.assertRaises(BufferError, resize, 0) |
---|
1355 | n/a | self.assertEqual(b, orig) |
---|
1356 | n/a | # Other operations implying resize |
---|
1357 | n/a | self.assertRaises(BufferError, b.pop, 0) |
---|
1358 | n/a | self.assertEqual(b, orig) |
---|
1359 | n/a | self.assertRaises(BufferError, b.remove, b[1]) |
---|
1360 | n/a | self.assertEqual(b, orig) |
---|
1361 | n/a | def delitem(): |
---|
1362 | n/a | del b[1] |
---|
1363 | n/a | self.assertRaises(BufferError, delitem) |
---|
1364 | n/a | self.assertEqual(b, orig) |
---|
1365 | n/a | # deleting a non-contiguous slice |
---|
1366 | n/a | def delslice(): |
---|
1367 | n/a | b[1:-1:2] = b"" |
---|
1368 | n/a | self.assertRaises(BufferError, delslice) |
---|
1369 | n/a | self.assertEqual(b, orig) |
---|
1370 | n/a | |
---|
1371 | n/a | @test.support.cpython_only |
---|
1372 | n/a | def test_obsolete_write_lock(self): |
---|
1373 | n/a | from _testcapi import getbuffer_with_null_view |
---|
1374 | n/a | self.assertRaises(BufferError, getbuffer_with_null_view, bytearray()) |
---|
1375 | n/a | |
---|
1376 | n/a | def test_iterator_pickling2(self): |
---|
1377 | n/a | orig = bytearray(b'abc') |
---|
1378 | n/a | data = list(b'qwerty') |
---|
1379 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
1380 | n/a | # initial iterator |
---|
1381 | n/a | itorig = iter(orig) |
---|
1382 | n/a | d = pickle.dumps((itorig, orig), proto) |
---|
1383 | n/a | it, b = pickle.loads(d) |
---|
1384 | n/a | b[:] = data |
---|
1385 | n/a | self.assertEqual(type(it), type(itorig)) |
---|
1386 | n/a | self.assertEqual(list(it), data) |
---|
1387 | n/a | |
---|
1388 | n/a | # running iterator |
---|
1389 | n/a | next(itorig) |
---|
1390 | n/a | d = pickle.dumps((itorig, orig), proto) |
---|
1391 | n/a | it, b = pickle.loads(d) |
---|
1392 | n/a | b[:] = data |
---|
1393 | n/a | self.assertEqual(type(it), type(itorig)) |
---|
1394 | n/a | self.assertEqual(list(it), data[1:]) |
---|
1395 | n/a | |
---|
1396 | n/a | # empty iterator |
---|
1397 | n/a | for i in range(1, len(orig)): |
---|
1398 | n/a | next(itorig) |
---|
1399 | n/a | d = pickle.dumps((itorig, orig), proto) |
---|
1400 | n/a | it, b = pickle.loads(d) |
---|
1401 | n/a | b[:] = data |
---|
1402 | n/a | self.assertEqual(type(it), type(itorig)) |
---|
1403 | n/a | self.assertEqual(list(it), data[len(orig):]) |
---|
1404 | n/a | |
---|
1405 | n/a | # exhausted iterator |
---|
1406 | n/a | self.assertRaises(StopIteration, next, itorig) |
---|
1407 | n/a | d = pickle.dumps((itorig, orig), proto) |
---|
1408 | n/a | it, b = pickle.loads(d) |
---|
1409 | n/a | b[:] = data |
---|
1410 | n/a | self.assertEqual(list(it), []) |
---|
1411 | n/a | |
---|
1412 | n/a | test_exhausted_iterator = test.list_tests.CommonTest.test_exhausted_iterator |
---|
1413 | n/a | |
---|
1414 | n/a | def test_iterator_length_hint(self): |
---|
1415 | n/a | # Issue 27443: __length_hint__ can return negative integer |
---|
1416 | n/a | ba = bytearray(b'ab') |
---|
1417 | n/a | it = iter(ba) |
---|
1418 | n/a | next(it) |
---|
1419 | n/a | ba.clear() |
---|
1420 | n/a | # Shouldn't raise an error |
---|
1421 | n/a | self.assertEqual(list(it), []) |
---|
1422 | n/a | |
---|
1423 | n/a | |
---|
1424 | n/a | class AssortedBytesTest(unittest.TestCase): |
---|
1425 | n/a | # |
---|
1426 | n/a | # Test various combinations of bytes and bytearray |
---|
1427 | n/a | # |
---|
1428 | n/a | |
---|
1429 | n/a | @check_bytes_warnings |
---|
1430 | n/a | def test_repr_str(self): |
---|
1431 | n/a | for f in str, repr: |
---|
1432 | n/a | self.assertEqual(f(bytearray()), "bytearray(b'')") |
---|
1433 | n/a | self.assertEqual(f(bytearray([0])), "bytearray(b'\\x00')") |
---|
1434 | n/a | self.assertEqual(f(bytearray([0, 1, 254, 255])), |
---|
1435 | n/a | "bytearray(b'\\x00\\x01\\xfe\\xff')") |
---|
1436 | n/a | self.assertEqual(f(b"abc"), "b'abc'") |
---|
1437 | n/a | self.assertEqual(f(b"'"), '''b"'"''') # ''' |
---|
1438 | n/a | self.assertEqual(f(b"'\""), r"""b'\'"'""") # ' |
---|
1439 | n/a | |
---|
1440 | n/a | @check_bytes_warnings |
---|
1441 | n/a | def test_format(self): |
---|
1442 | n/a | for b in b'abc', bytearray(b'abc'): |
---|
1443 | n/a | self.assertEqual(format(b), str(b)) |
---|
1444 | n/a | self.assertEqual(format(b, ''), str(b)) |
---|
1445 | n/a | with self.assertRaisesRegex(TypeError, |
---|
1446 | n/a | r'\b%s\b' % re.escape(type(b).__name__)): |
---|
1447 | n/a | format(b, 's') |
---|
1448 | n/a | |
---|
1449 | n/a | def test_compare_bytes_to_bytearray(self): |
---|
1450 | n/a | self.assertEqual(b"abc" == bytes(b"abc"), True) |
---|
1451 | n/a | self.assertEqual(b"ab" != bytes(b"abc"), True) |
---|
1452 | n/a | self.assertEqual(b"ab" <= bytes(b"abc"), True) |
---|
1453 | n/a | self.assertEqual(b"ab" < bytes(b"abc"), True) |
---|
1454 | n/a | self.assertEqual(b"abc" >= bytes(b"ab"), True) |
---|
1455 | n/a | self.assertEqual(b"abc" > bytes(b"ab"), True) |
---|
1456 | n/a | |
---|
1457 | n/a | self.assertEqual(b"abc" != bytes(b"abc"), False) |
---|
1458 | n/a | self.assertEqual(b"ab" == bytes(b"abc"), False) |
---|
1459 | n/a | self.assertEqual(b"ab" > bytes(b"abc"), False) |
---|
1460 | n/a | self.assertEqual(b"ab" >= bytes(b"abc"), False) |
---|
1461 | n/a | self.assertEqual(b"abc" < bytes(b"ab"), False) |
---|
1462 | n/a | self.assertEqual(b"abc" <= bytes(b"ab"), False) |
---|
1463 | n/a | |
---|
1464 | n/a | self.assertEqual(bytes(b"abc") == b"abc", True) |
---|
1465 | n/a | self.assertEqual(bytes(b"ab") != b"abc", True) |
---|
1466 | n/a | self.assertEqual(bytes(b"ab") <= b"abc", True) |
---|
1467 | n/a | self.assertEqual(bytes(b"ab") < b"abc", True) |
---|
1468 | n/a | self.assertEqual(bytes(b"abc") >= b"ab", True) |
---|
1469 | n/a | self.assertEqual(bytes(b"abc") > b"ab", True) |
---|
1470 | n/a | |
---|
1471 | n/a | self.assertEqual(bytes(b"abc") != b"abc", False) |
---|
1472 | n/a | self.assertEqual(bytes(b"ab") == b"abc", False) |
---|
1473 | n/a | self.assertEqual(bytes(b"ab") > b"abc", False) |
---|
1474 | n/a | self.assertEqual(bytes(b"ab") >= b"abc", False) |
---|
1475 | n/a | self.assertEqual(bytes(b"abc") < b"ab", False) |
---|
1476 | n/a | self.assertEqual(bytes(b"abc") <= b"ab", False) |
---|
1477 | n/a | |
---|
1478 | n/a | @test.support.requires_docstrings |
---|
1479 | n/a | def test_doc(self): |
---|
1480 | n/a | self.assertIsNotNone(bytearray.__doc__) |
---|
1481 | n/a | self.assertTrue(bytearray.__doc__.startswith("bytearray("), bytearray.__doc__) |
---|
1482 | n/a | self.assertIsNotNone(bytes.__doc__) |
---|
1483 | n/a | self.assertTrue(bytes.__doc__.startswith("bytes("), bytes.__doc__) |
---|
1484 | n/a | |
---|
1485 | n/a | def test_from_bytearray(self): |
---|
1486 | n/a | sample = bytes(b"Hello world\n\x80\x81\xfe\xff") |
---|
1487 | n/a | buf = memoryview(sample) |
---|
1488 | n/a | b = bytearray(buf) |
---|
1489 | n/a | self.assertEqual(b, bytearray(sample)) |
---|
1490 | n/a | |
---|
1491 | n/a | @check_bytes_warnings |
---|
1492 | n/a | def test_to_str(self): |
---|
1493 | n/a | self.assertEqual(str(b''), "b''") |
---|
1494 | n/a | self.assertEqual(str(b'x'), "b'x'") |
---|
1495 | n/a | self.assertEqual(str(b'\x80'), "b'\\x80'") |
---|
1496 | n/a | self.assertEqual(str(bytearray(b'')), "bytearray(b'')") |
---|
1497 | n/a | self.assertEqual(str(bytearray(b'x')), "bytearray(b'x')") |
---|
1498 | n/a | self.assertEqual(str(bytearray(b'\x80')), "bytearray(b'\\x80')") |
---|
1499 | n/a | |
---|
1500 | n/a | def test_literal(self): |
---|
1501 | n/a | tests = [ |
---|
1502 | n/a | (b"Wonderful spam", "Wonderful spam"), |
---|
1503 | n/a | (br"Wonderful spam too", "Wonderful spam too"), |
---|
1504 | n/a | (b"\xaa\x00\000\200", "\xaa\x00\000\200"), |
---|
1505 | n/a | (br"\xaa\x00\000\200", r"\xaa\x00\000\200"), |
---|
1506 | n/a | ] |
---|
1507 | n/a | for b, s in tests: |
---|
1508 | n/a | self.assertEqual(b, bytearray(s, 'latin-1')) |
---|
1509 | n/a | for c in range(128, 256): |
---|
1510 | n/a | self.assertRaises(SyntaxError, eval, |
---|
1511 | n/a | 'b"%s"' % chr(c)) |
---|
1512 | n/a | |
---|
1513 | n/a | def test_split_bytearray(self): |
---|
1514 | n/a | self.assertEqual(b'a b'.split(memoryview(b' ')), [b'a', b'b']) |
---|
1515 | n/a | |
---|
1516 | n/a | def test_rsplit_bytearray(self): |
---|
1517 | n/a | self.assertEqual(b'a b'.rsplit(memoryview(b' ')), [b'a', b'b']) |
---|
1518 | n/a | |
---|
1519 | n/a | def test_return_self(self): |
---|
1520 | n/a | # bytearray.replace must always return a new bytearray |
---|
1521 | n/a | b = bytearray() |
---|
1522 | n/a | self.assertFalse(b.replace(b'', b'') is b) |
---|
1523 | n/a | |
---|
1524 | n/a | @unittest.skipUnless(sys.flags.bytes_warning, |
---|
1525 | n/a | "BytesWarning is needed for this test: use -bb option") |
---|
1526 | n/a | def test_compare(self): |
---|
1527 | n/a | def bytes_warning(): |
---|
1528 | n/a | return test.support.check_warnings(('', BytesWarning)) |
---|
1529 | n/a | with bytes_warning(): |
---|
1530 | n/a | b'' == '' |
---|
1531 | n/a | with bytes_warning(): |
---|
1532 | n/a | '' == b'' |
---|
1533 | n/a | with bytes_warning(): |
---|
1534 | n/a | b'' != '' |
---|
1535 | n/a | with bytes_warning(): |
---|
1536 | n/a | '' != b'' |
---|
1537 | n/a | with bytes_warning(): |
---|
1538 | n/a | bytearray(b'') == '' |
---|
1539 | n/a | with bytes_warning(): |
---|
1540 | n/a | '' == bytearray(b'') |
---|
1541 | n/a | with bytes_warning(): |
---|
1542 | n/a | bytearray(b'') != '' |
---|
1543 | n/a | with bytes_warning(): |
---|
1544 | n/a | '' != bytearray(b'') |
---|
1545 | n/a | with bytes_warning(): |
---|
1546 | n/a | b'\0' == 0 |
---|
1547 | n/a | with bytes_warning(): |
---|
1548 | n/a | 0 == b'\0' |
---|
1549 | n/a | with bytes_warning(): |
---|
1550 | n/a | b'\0' != 0 |
---|
1551 | n/a | with bytes_warning(): |
---|
1552 | n/a | 0 != b'\0' |
---|
1553 | n/a | |
---|
1554 | n/a | # Optimizations: |
---|
1555 | n/a | # __iter__? (optimization) |
---|
1556 | n/a | # __reversed__? (optimization) |
---|
1557 | n/a | |
---|
1558 | n/a | # XXX More string methods? (Those that don't use character properties) |
---|
1559 | n/a | |
---|
1560 | n/a | # There are tests in string_tests.py that are more |
---|
1561 | n/a | # comprehensive for things like partition, etc. |
---|
1562 | n/a | # Unfortunately they are all bundled with tests that |
---|
1563 | n/a | # are not appropriate for bytes |
---|
1564 | n/a | |
---|
1565 | n/a | # I've started porting some of those into bytearray_tests.py, we should port |
---|
1566 | n/a | # the rest that make sense (the code can be cleaned up to use modern |
---|
1567 | n/a | # unittest methods at the same time). |
---|
1568 | n/a | |
---|
1569 | n/a | class BytearrayPEP3137Test(unittest.TestCase): |
---|
1570 | n/a | def marshal(self, x): |
---|
1571 | n/a | return bytearray(x) |
---|
1572 | n/a | |
---|
1573 | n/a | def test_returns_new_copy(self): |
---|
1574 | n/a | val = self.marshal(b'1234') |
---|
1575 | n/a | # On immutable types these MAY return a reference to themselves |
---|
1576 | n/a | # but on mutable types like bytearray they MUST return a new copy. |
---|
1577 | n/a | for methname in ('zfill', 'rjust', 'ljust', 'center'): |
---|
1578 | n/a | method = getattr(val, methname) |
---|
1579 | n/a | newval = method(3) |
---|
1580 | n/a | self.assertEqual(val, newval) |
---|
1581 | n/a | self.assertTrue(val is not newval, |
---|
1582 | n/a | methname+' returned self on a mutable object') |
---|
1583 | n/a | for expr in ('val.split()[0]', 'val.rsplit()[0]', |
---|
1584 | n/a | 'val.partition(b".")[0]', 'val.rpartition(b".")[2]', |
---|
1585 | n/a | 'val.splitlines()[0]', 'val.replace(b"", b"")'): |
---|
1586 | n/a | newval = eval(expr) |
---|
1587 | n/a | self.assertEqual(val, newval) |
---|
1588 | n/a | self.assertTrue(val is not newval, |
---|
1589 | n/a | expr+' returned val on a mutable object') |
---|
1590 | n/a | sep = self.marshal(b'') |
---|
1591 | n/a | newval = sep.join([val]) |
---|
1592 | n/a | self.assertEqual(val, newval) |
---|
1593 | n/a | self.assertIsNot(val, newval) |
---|
1594 | n/a | |
---|
1595 | n/a | |
---|
1596 | n/a | class FixedStringTest(test.string_tests.BaseTest): |
---|
1597 | n/a | def fixtype(self, obj): |
---|
1598 | n/a | if isinstance(obj, str): |
---|
1599 | n/a | return self.type2test(obj.encode("utf-8")) |
---|
1600 | n/a | return super().fixtype(obj) |
---|
1601 | n/a | |
---|
1602 | n/a | contains_bytes = True |
---|
1603 | n/a | |
---|
1604 | n/a | class ByteArrayAsStringTest(FixedStringTest, unittest.TestCase): |
---|
1605 | n/a | type2test = bytearray |
---|
1606 | n/a | |
---|
1607 | n/a | class BytesAsStringTest(FixedStringTest, unittest.TestCase): |
---|
1608 | n/a | type2test = bytes |
---|
1609 | n/a | |
---|
1610 | n/a | |
---|
1611 | n/a | class SubclassTest: |
---|
1612 | n/a | |
---|
1613 | n/a | def test_basic(self): |
---|
1614 | n/a | self.assertTrue(issubclass(self.type2test, self.basetype)) |
---|
1615 | n/a | self.assertIsInstance(self.type2test(), self.basetype) |
---|
1616 | n/a | |
---|
1617 | n/a | a, b = b"abcd", b"efgh" |
---|
1618 | n/a | _a, _b = self.type2test(a), self.type2test(b) |
---|
1619 | n/a | |
---|
1620 | n/a | # test comparison operators with subclass instances |
---|
1621 | n/a | self.assertTrue(_a == _a) |
---|
1622 | n/a | self.assertTrue(_a != _b) |
---|
1623 | n/a | self.assertTrue(_a < _b) |
---|
1624 | n/a | self.assertTrue(_a <= _b) |
---|
1625 | n/a | self.assertTrue(_b >= _a) |
---|
1626 | n/a | self.assertTrue(_b > _a) |
---|
1627 | n/a | self.assertTrue(_a is not a) |
---|
1628 | n/a | |
---|
1629 | n/a | # test concat of subclass instances |
---|
1630 | n/a | self.assertEqual(a + b, _a + _b) |
---|
1631 | n/a | self.assertEqual(a + b, a + _b) |
---|
1632 | n/a | self.assertEqual(a + b, _a + b) |
---|
1633 | n/a | |
---|
1634 | n/a | # test repeat |
---|
1635 | n/a | self.assertTrue(a*5 == _a*5) |
---|
1636 | n/a | |
---|
1637 | n/a | def test_join(self): |
---|
1638 | n/a | # Make sure join returns a NEW object for single item sequences |
---|
1639 | n/a | # involving a subclass. |
---|
1640 | n/a | # Make sure that it is of the appropriate type. |
---|
1641 | n/a | s1 = self.type2test(b"abcd") |
---|
1642 | n/a | s2 = self.basetype().join([s1]) |
---|
1643 | n/a | self.assertTrue(s1 is not s2) |
---|
1644 | n/a | self.assertTrue(type(s2) is self.basetype, type(s2)) |
---|
1645 | n/a | |
---|
1646 | n/a | # Test reverse, calling join on subclass |
---|
1647 | n/a | s3 = s1.join([b"abcd"]) |
---|
1648 | n/a | self.assertTrue(type(s3) is self.basetype) |
---|
1649 | n/a | |
---|
1650 | n/a | def test_pickle(self): |
---|
1651 | n/a | a = self.type2test(b"abcd") |
---|
1652 | n/a | a.x = 10 |
---|
1653 | n/a | a.y = self.type2test(b"efgh") |
---|
1654 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
1655 | n/a | b = pickle.loads(pickle.dumps(a, proto)) |
---|
1656 | n/a | self.assertNotEqual(id(a), id(b)) |
---|
1657 | n/a | self.assertEqual(a, b) |
---|
1658 | n/a | self.assertEqual(a.x, b.x) |
---|
1659 | n/a | self.assertEqual(a.y, b.y) |
---|
1660 | n/a | self.assertEqual(type(a), type(b)) |
---|
1661 | n/a | self.assertEqual(type(a.y), type(b.y)) |
---|
1662 | n/a | |
---|
1663 | n/a | def test_copy(self): |
---|
1664 | n/a | a = self.type2test(b"abcd") |
---|
1665 | n/a | a.x = 10 |
---|
1666 | n/a | a.y = self.type2test(b"efgh") |
---|
1667 | n/a | for copy_method in (copy.copy, copy.deepcopy): |
---|
1668 | n/a | b = copy_method(a) |
---|
1669 | n/a | self.assertNotEqual(id(a), id(b)) |
---|
1670 | n/a | self.assertEqual(a, b) |
---|
1671 | n/a | self.assertEqual(a.x, b.x) |
---|
1672 | n/a | self.assertEqual(a.y, b.y) |
---|
1673 | n/a | self.assertEqual(type(a), type(b)) |
---|
1674 | n/a | self.assertEqual(type(a.y), type(b.y)) |
---|
1675 | n/a | |
---|
1676 | n/a | def test_fromhex(self): |
---|
1677 | n/a | b = self.type2test.fromhex('1a2B30') |
---|
1678 | n/a | self.assertEqual(b, b'\x1a\x2b\x30') |
---|
1679 | n/a | self.assertIs(type(b), self.type2test) |
---|
1680 | n/a | |
---|
1681 | n/a | class B1(self.basetype): |
---|
1682 | n/a | def __new__(cls, value): |
---|
1683 | n/a | me = self.basetype.__new__(cls, value) |
---|
1684 | n/a | me.foo = 'bar' |
---|
1685 | n/a | return me |
---|
1686 | n/a | |
---|
1687 | n/a | b = B1.fromhex('1a2B30') |
---|
1688 | n/a | self.assertEqual(b, b'\x1a\x2b\x30') |
---|
1689 | n/a | self.assertIs(type(b), B1) |
---|
1690 | n/a | self.assertEqual(b.foo, 'bar') |
---|
1691 | n/a | |
---|
1692 | n/a | class B2(self.basetype): |
---|
1693 | n/a | def __init__(me, *args, **kwargs): |
---|
1694 | n/a | if self.basetype is not bytes: |
---|
1695 | n/a | self.basetype.__init__(me, *args, **kwargs) |
---|
1696 | n/a | me.foo = 'bar' |
---|
1697 | n/a | |
---|
1698 | n/a | b = B2.fromhex('1a2B30') |
---|
1699 | n/a | self.assertEqual(b, b'\x1a\x2b\x30') |
---|
1700 | n/a | self.assertIs(type(b), B2) |
---|
1701 | n/a | self.assertEqual(b.foo, 'bar') |
---|
1702 | n/a | |
---|
1703 | n/a | |
---|
1704 | n/a | class ByteArraySubclass(bytearray): |
---|
1705 | n/a | pass |
---|
1706 | n/a | |
---|
1707 | n/a | class BytesSubclass(bytes): |
---|
1708 | n/a | pass |
---|
1709 | n/a | |
---|
1710 | n/a | class OtherBytesSubclass(bytes): |
---|
1711 | n/a | pass |
---|
1712 | n/a | |
---|
1713 | n/a | class ByteArraySubclassTest(SubclassTest, unittest.TestCase): |
---|
1714 | n/a | basetype = bytearray |
---|
1715 | n/a | type2test = ByteArraySubclass |
---|
1716 | n/a | |
---|
1717 | n/a | def test_init_override(self): |
---|
1718 | n/a | class subclass(bytearray): |
---|
1719 | n/a | def __init__(me, newarg=1, *args, **kwargs): |
---|
1720 | n/a | bytearray.__init__(me, *args, **kwargs) |
---|
1721 | n/a | x = subclass(4, b"abcd") |
---|
1722 | n/a | x = subclass(4, source=b"abcd") |
---|
1723 | n/a | self.assertEqual(x, b"abcd") |
---|
1724 | n/a | x = subclass(newarg=4, source=b"abcd") |
---|
1725 | n/a | self.assertEqual(x, b"abcd") |
---|
1726 | n/a | |
---|
1727 | n/a | |
---|
1728 | n/a | class BytesSubclassTest(SubclassTest, unittest.TestCase): |
---|
1729 | n/a | basetype = bytes |
---|
1730 | n/a | type2test = BytesSubclass |
---|
1731 | n/a | |
---|
1732 | n/a | |
---|
1733 | n/a | if __name__ == "__main__": |
---|
1734 | n/a | unittest.main() |
---|