blob: 2813d96e80aef9abdad2a5cc4d05582e2bccd7f3 [file] [log] [blame]
Guido van Rossum254348e2007-11-21 19:29:53 +00001"""Unit tests for the bytes and bytearray types.
Guido van Rossum98297ee2007-11-06 21:34:58 +00002
3XXX This is a mess. Common tests should be moved to buffer_tests.py,
4which itself ought to be unified with string_tests.py (and the latter
5should be modernized).
6"""
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00007
Guido van Rossumd624f182006-04-24 13:47:05 +00008import os
9import re
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000010import sys
Guido van Rossuma6c04be2007-11-03 00:24:24 +000011import copy
Barry Warsaw9e9dcd62008-10-17 01:50:37 +000012import operator
Guido van Rossum0ad08122007-04-11 04:37:43 +000013import pickle
Guido van Rossumd624f182006-04-24 13:47:05 +000014import tempfile
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000015import unittest
Guido van Rossum98297ee2007-11-06 21:34:58 +000016import warnings
Benjamin Petersonee8712c2008-05-20 21:35:26 +000017import test.support
Georg Brandlc7885542007-03-06 19:16:20 +000018import test.string_tests
Gregory P. Smith60d241f2007-10-16 06:31:30 +000019import test.buffer_tests
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000020
Georg Brandl9a54d7c2008-07-16 23:15:30 +000021class Indexable:
22 def __init__(self, value=0):
23 self.value = value
24 def __index__(self):
25 return self.value
26
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000027
Christian Heimes510711d2008-01-30 11:57:58 +000028class BaseBytesTest(unittest.TestCase):
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000029
Guido van Rossum98297ee2007-11-06 21:34:58 +000030 def setUp(self):
31 self.warning_filters = warnings.filters[:]
32
33 def tearDown(self):
34 warnings.filters = self.warning_filters
35
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000036 def test_basics(self):
Christian Heimes510711d2008-01-30 11:57:58 +000037 b = self.type2test()
38 self.assertEqual(type(b), self.type2test)
39 self.assertEqual(b.__class__, self.type2test)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000040
Alexandre Vassalotti41f58a72010-01-12 01:23:09 +000041 def test_copy(self):
42 a = self.type2test(b"abcd")
43 for copy_method in (copy.copy, copy.deepcopy):
44 b = copy_method(a)
45 self.assertEqual(a, b)
46 self.assertEqual(type(a), type(b))
47
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000048 def test_empty_sequence(self):
Christian Heimes510711d2008-01-30 11:57:58 +000049 b = self.type2test()
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000050 self.assertEqual(len(b), 0)
51 self.assertRaises(IndexError, lambda: b[0])
52 self.assertRaises(IndexError, lambda: b[1])
Christian Heimesa37d4c62007-12-04 23:02:19 +000053 self.assertRaises(IndexError, lambda: b[sys.maxsize])
54 self.assertRaises(IndexError, lambda: b[sys.maxsize+1])
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000055 self.assertRaises(IndexError, lambda: b[10**100])
56 self.assertRaises(IndexError, lambda: b[-1])
57 self.assertRaises(IndexError, lambda: b[-2])
Christian Heimesa37d4c62007-12-04 23:02:19 +000058 self.assertRaises(IndexError, lambda: b[-sys.maxsize])
59 self.assertRaises(IndexError, lambda: b[-sys.maxsize-1])
60 self.assertRaises(IndexError, lambda: b[-sys.maxsize-2])
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000061 self.assertRaises(IndexError, lambda: b[-10**100])
62
63 def test_from_list(self):
64 ints = list(range(256))
Christian Heimes510711d2008-01-30 11:57:58 +000065 b = self.type2test(i for i in ints)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000066 self.assertEqual(len(b), 256)
67 self.assertEqual(list(b), ints)
68
69 def test_from_index(self):
Georg Brandl9a54d7c2008-07-16 23:15:30 +000070 b = self.type2test([Indexable(), Indexable(1), Indexable(254),
71 Indexable(255)])
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000072 self.assertEqual(list(b), [0, 1, 254, 255])
Georg Brandl9a54d7c2008-07-16 23:15:30 +000073 self.assertRaises(ValueError, bytearray, [Indexable(-1)])
74 self.assertRaises(ValueError, bytearray, [Indexable(256)])
Guido van Rossum98297ee2007-11-06 21:34:58 +000075
76 def test_from_ssize(self):
Guido van Rossum254348e2007-11-21 19:29:53 +000077 self.assertEqual(bytearray(0), b'')
78 self.assertEqual(bytearray(1), b'\x00')
79 self.assertEqual(bytearray(5), b'\x00\x00\x00\x00\x00')
80 self.assertRaises(ValueError, bytearray, -1)
Guido van Rossum98297ee2007-11-06 21:34:58 +000081
Guido van Rossum254348e2007-11-21 19:29:53 +000082 self.assertEqual(bytearray('0', 'ascii'), b'0')
83 self.assertEqual(bytearray(b'0'), b'0')
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000084
85 def test_constructor_type_errors(self):
Christian Heimes510711d2008-01-30 11:57:58 +000086 self.assertRaises(TypeError, self.type2test, 0.0)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000087 class C:
88 pass
Christian Heimes510711d2008-01-30 11:57:58 +000089 self.assertRaises(TypeError, self.type2test, ["0"])
90 self.assertRaises(TypeError, self.type2test, [0.0])
91 self.assertRaises(TypeError, self.type2test, [None])
92 self.assertRaises(TypeError, self.type2test, [C()])
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000093
94 def test_constructor_value_errors(self):
Christian Heimes510711d2008-01-30 11:57:58 +000095 self.assertRaises(ValueError, self.type2test, [-1])
96 self.assertRaises(ValueError, self.type2test, [-sys.maxsize])
97 self.assertRaises(ValueError, self.type2test, [-sys.maxsize-1])
98 self.assertRaises(ValueError, self.type2test, [-sys.maxsize-2])
99 self.assertRaises(ValueError, self.type2test, [-10**100])
100 self.assertRaises(ValueError, self.type2test, [256])
101 self.assertRaises(ValueError, self.type2test, [257])
102 self.assertRaises(ValueError, self.type2test, [sys.maxsize])
103 self.assertRaises(ValueError, self.type2test, [sys.maxsize+1])
104 self.assertRaises(ValueError, self.type2test, [10**100])
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000105
106 def test_compare(self):
Christian Heimes510711d2008-01-30 11:57:58 +0000107 b1 = self.type2test([1, 2, 3])
108 b2 = self.type2test([1, 2, 3])
109 b3 = self.type2test([1, 3])
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000110
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000111 self.assertEqual(b1, b2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000112 self.assertTrue(b2 != b3)
113 self.assertTrue(b1 <= b2)
114 self.assertTrue(b1 <= b3)
115 self.assertTrue(b1 < b3)
116 self.assertTrue(b1 >= b2)
117 self.assertTrue(b3 >= b2)
118 self.assertTrue(b3 > b2)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000119
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000120 self.assertFalse(b1 != b2)
121 self.assertFalse(b2 == b3)
122 self.assertFalse(b1 > b2)
123 self.assertFalse(b1 > b3)
124 self.assertFalse(b1 >= b3)
125 self.assertFalse(b1 < b2)
126 self.assertFalse(b3 < b2)
127 self.assertFalse(b3 <= b2)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000128
Guido van Rossum343e97f2007-04-09 00:43:24 +0000129 def test_compare_to_str(self):
Guido van Rossum98297ee2007-11-06 21:34:58 +0000130 warnings.simplefilter('ignore', BytesWarning)
Guido van Rossum1e35e762007-10-09 17:21:10 +0000131 # Byte comparisons with unicode should always fail!
Guido van Rossumebea9be2007-04-09 00:49:13 +0000132 # Test this for all expected byte orders and Unicode character sizes
Christian Heimes510711d2008-01-30 11:57:58 +0000133 self.assertEqual(self.type2test(b"\0a\0b\0c") == "abc", False)
134 self.assertEqual(self.type2test(b"\0\0\0a\0\0\0b\0\0\0c") == "abc", False)
135 self.assertEqual(self.type2test(b"a\0b\0c\0") == "abc", False)
136 self.assertEqual(self.type2test(b"a\0\0\0b\0\0\0c\0\0\0") == "abc", False)
137 self.assertEqual(self.type2test() == str(), False)
138 self.assertEqual(self.type2test() != str(), True)
139
140 def test_reversed(self):
141 input = list(map(ord, "Hello"))
142 b = self.type2test(input)
143 output = list(reversed(b))
144 input.reverse()
145 self.assertEqual(output, input)
146
147 def test_getslice(self):
148 def by(s):
149 return self.type2test(map(ord, s))
150 b = by("Hello, world")
151
152 self.assertEqual(b[:5], by("Hello"))
153 self.assertEqual(b[1:5], by("ello"))
154 self.assertEqual(b[5:7], by(", "))
155 self.assertEqual(b[7:], by("world"))
156 self.assertEqual(b[7:12], by("world"))
157 self.assertEqual(b[7:100], by("world"))
158
159 self.assertEqual(b[:-7], by("Hello"))
160 self.assertEqual(b[-11:-7], by("ello"))
161 self.assertEqual(b[-7:-5], by(", "))
162 self.assertEqual(b[-5:], by("world"))
163 self.assertEqual(b[-5:12], by("world"))
164 self.assertEqual(b[-5:100], by("world"))
165 self.assertEqual(b[-100:5], by("Hello"))
166
167 def test_extended_getslice(self):
168 # Test extended slicing by comparing with list slicing.
169 L = list(range(255))
170 b = self.type2test(L)
171 indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100)
172 for start in indices:
173 for stop in indices:
174 # Skip step 0 (invalid)
175 for step in indices[1:]:
176 self.assertEqual(b[start:stop:step], self.type2test(L[start:stop:step]))
177
178 def test_encoding(self):
Martin v. Löwisdb12d452009-05-02 18:52:14 +0000179 sample = "Hello world\n\u1234\u5678\u9abc"
Christian Heimes510711d2008-01-30 11:57:58 +0000180 for enc in ("utf8", "utf16"):
181 b = self.type2test(sample, enc)
182 self.assertEqual(b, self.type2test(sample.encode(enc)))
183 self.assertRaises(UnicodeEncodeError, self.type2test, sample, "latin1")
184 b = self.type2test(sample, "latin1", "ignore")
Martin v. Löwisdb12d452009-05-02 18:52:14 +0000185 self.assertEqual(b, self.type2test(sample[:-3], "utf-8"))
Christian Heimes510711d2008-01-30 11:57:58 +0000186
187 def test_decode(self):
188 sample = "Hello world\n\u1234\u5678\u9abc\def0\def0"
189 for enc in ("utf8", "utf16"):
190 b = self.type2test(sample, enc)
191 self.assertEqual(b.decode(enc), sample)
192 sample = "Hello world\n\x80\x81\xfe\xff"
193 b = self.type2test(sample, "latin1")
194 self.assertRaises(UnicodeDecodeError, b.decode, "utf8")
195 self.assertEqual(b.decode("utf8", "ignore"), "Hello world\n")
Benjamin Peterson308d6372009-09-18 21:42:35 +0000196 self.assertEqual(b.decode(errors="ignore", encoding="utf8"),
197 "Hello world\n")
Christian Heimes510711d2008-01-30 11:57:58 +0000198
199 def test_from_int(self):
200 b = self.type2test(0)
201 self.assertEqual(b, self.type2test())
202 b = self.type2test(10)
203 self.assertEqual(b, self.type2test([0]*10))
204 b = self.type2test(10000)
205 self.assertEqual(b, self.type2test([0]*10000))
206
207 def test_concat(self):
208 b1 = self.type2test(b"abc")
209 b2 = self.type2test(b"def")
210 self.assertEqual(b1 + b2, b"abcdef")
211 self.assertEqual(b1 + bytes(b"def"), b"abcdef")
212 self.assertEqual(bytes(b"def") + b1, b"defabc")
213 self.assertRaises(TypeError, lambda: b1 + "def")
214 self.assertRaises(TypeError, lambda: "abc" + b2)
215
216 def test_repeat(self):
217 for b in b"abc", self.type2test(b"abc"):
218 self.assertEqual(b * 3, b"abcabcabc")
219 self.assertEqual(b * 0, b"")
220 self.assertEqual(b * -1, b"")
221 self.assertRaises(TypeError, lambda: b * 3.14)
222 self.assertRaises(TypeError, lambda: 3.14 * b)
223 # XXX Shouldn't bytes and bytearray agree on what to raise?
224 self.assertRaises((OverflowError, MemoryError),
225 lambda: b * sys.maxsize)
226
227 def test_repeat_1char(self):
228 self.assertEqual(self.type2test(b'x')*100, self.type2test([ord('x')]*100))
229
230 def test_contains(self):
231 b = self.type2test(b"abc")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000232 self.assertTrue(ord('a') in b)
233 self.assertTrue(int(ord('a')) in b)
234 self.assertFalse(200 in b)
235 self.assertFalse(200 in b)
Christian Heimes510711d2008-01-30 11:57:58 +0000236 self.assertRaises(ValueError, lambda: 300 in b)
237 self.assertRaises(ValueError, lambda: -1 in b)
238 self.assertRaises(TypeError, lambda: None in b)
239 self.assertRaises(TypeError, lambda: float(ord('a')) in b)
240 self.assertRaises(TypeError, lambda: "a" in b)
241 for f in bytes, bytearray:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000242 self.assertTrue(f(b"") in b)
243 self.assertTrue(f(b"a") in b)
244 self.assertTrue(f(b"b") in b)
245 self.assertTrue(f(b"c") in b)
246 self.assertTrue(f(b"ab") in b)
247 self.assertTrue(f(b"bc") in b)
248 self.assertTrue(f(b"abc") in b)
249 self.assertFalse(f(b"ac") in b)
250 self.assertFalse(f(b"d") in b)
251 self.assertFalse(f(b"dab") in b)
252 self.assertFalse(f(b"abd") in b)
Christian Heimes510711d2008-01-30 11:57:58 +0000253
254 def test_fromhex(self):
255 self.assertRaises(TypeError, self.type2test.fromhex)
256 self.assertRaises(TypeError, self.type2test.fromhex, 1)
Christian Heimes4f066122008-01-30 15:02:52 +0000257 self.assertEquals(self.type2test.fromhex(''), self.type2test())
Christian Heimes510711d2008-01-30 11:57:58 +0000258 b = bytearray([0x1a, 0x2b, 0x30])
259 self.assertEquals(self.type2test.fromhex('1a2B30'), b)
260 self.assertEquals(self.type2test.fromhex(' 1A 2B 30 '), b)
261 self.assertEquals(self.type2test.fromhex('0000'), b'\0\0')
262 self.assertRaises(TypeError, self.type2test.fromhex, b'1B')
263 self.assertRaises(ValueError, self.type2test.fromhex, 'a')
264 self.assertRaises(ValueError, self.type2test.fromhex, 'rt')
265 self.assertRaises(ValueError, self.type2test.fromhex, '1a b cd')
266 self.assertRaises(ValueError, self.type2test.fromhex, '\x00')
267 self.assertRaises(ValueError, self.type2test.fromhex, '12 \x00 34')
268
269 def test_join(self):
270 self.assertEqual(self.type2test(b"").join([]), b"")
271 self.assertEqual(self.type2test(b"").join([b""]), b"")
272 for lst in [[b"abc"], [b"a", b"bc"], [b"ab", b"c"], [b"a", b"b", b"c"]]:
273 lst = list(map(self.type2test, lst))
274 self.assertEqual(self.type2test(b"").join(lst), b"abc")
275 self.assertEqual(self.type2test(b"").join(tuple(lst)), b"abc")
276 self.assertEqual(self.type2test(b"").join(iter(lst)), b"abc")
277 self.assertEqual(self.type2test(b".").join([b"ab", b"cd"]), b"ab.cd")
278 # XXX more...
279
Christian Heimes510711d2008-01-30 11:57:58 +0000280 def test_count(self):
281 b = self.type2test(b'mississippi')
282 self.assertEqual(b.count(b'i'), 4)
283 self.assertEqual(b.count(b'ss'), 2)
284 self.assertEqual(b.count(b'w'), 0)
285
286 def test_startswith(self):
287 b = self.type2test(b'hello')
288 self.assertFalse(self.type2test().startswith(b"anything"))
289 self.assertTrue(b.startswith(b"hello"))
290 self.assertTrue(b.startswith(b"hel"))
291 self.assertTrue(b.startswith(b"h"))
292 self.assertFalse(b.startswith(b"hellow"))
293 self.assertFalse(b.startswith(b"ha"))
294
295 def test_endswith(self):
296 b = self.type2test(b'hello')
297 self.assertFalse(bytearray().endswith(b"anything"))
298 self.assertTrue(b.endswith(b"hello"))
299 self.assertTrue(b.endswith(b"llo"))
300 self.assertTrue(b.endswith(b"o"))
301 self.assertFalse(b.endswith(b"whello"))
302 self.assertFalse(b.endswith(b"no"))
303
304 def test_find(self):
305 b = self.type2test(b'mississippi')
306 self.assertEqual(b.find(b'ss'), 2)
307 self.assertEqual(b.find(b'ss', 3), 5)
308 self.assertEqual(b.find(b'ss', 1, 7), 2)
309 self.assertEqual(b.find(b'ss', 1, 3), -1)
310 self.assertEqual(b.find(b'w'), -1)
311 self.assertEqual(b.find(b'mississippian'), -1)
312
313 def test_rfind(self):
314 b = self.type2test(b'mississippi')
315 self.assertEqual(b.rfind(b'ss'), 5)
316 self.assertEqual(b.rfind(b'ss', 3), 5)
317 self.assertEqual(b.rfind(b'ss', 0, 6), 2)
318 self.assertEqual(b.rfind(b'w'), -1)
319 self.assertEqual(b.rfind(b'mississippian'), -1)
320
321 def test_index(self):
322 b = self.type2test(b'world')
323 self.assertEqual(b.index(b'w'), 0)
324 self.assertEqual(b.index(b'orl'), 1)
325 self.assertRaises(ValueError, b.index, b'worm')
326 self.assertRaises(ValueError, b.index, b'ldo')
327
328 def test_rindex(self):
329 # XXX could be more rigorous
330 b = self.type2test(b'world')
331 self.assertEqual(b.rindex(b'w'), 0)
332 self.assertEqual(b.rindex(b'orl'), 1)
333 self.assertRaises(ValueError, b.rindex, b'worm')
334 self.assertRaises(ValueError, b.rindex, b'ldo')
335
336 def test_replace(self):
337 b = self.type2test(b'mississippi')
338 self.assertEqual(b.replace(b'i', b'a'), b'massassappa')
339 self.assertEqual(b.replace(b'ss', b'x'), b'mixixippi')
340
341 def test_split(self):
342 b = self.type2test(b'mississippi')
343 self.assertEqual(b.split(b'i'), [b'm', b'ss', b'ss', b'pp', b''])
344 self.assertEqual(b.split(b'ss'), [b'mi', b'i', b'ippi'])
345 self.assertEqual(b.split(b'w'), [b])
346
347 def test_split_whitespace(self):
348 for b in (b' arf barf ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf',
349 b'arf\fbarf', b'arf\vbarf'):
350 b = self.type2test(b)
351 self.assertEqual(b.split(), [b'arf', b'barf'])
352 self.assertEqual(b.split(None), [b'arf', b'barf'])
353 self.assertEqual(b.split(None, 2), [b'arf', b'barf'])
354 for b in (b'a\x1Cb', b'a\x1Db', b'a\x1Eb', b'a\x1Fb'):
355 b = self.type2test(b)
356 self.assertEqual(b.split(), [b])
357 self.assertEqual(self.type2test(b' a bb c ').split(None, 0), [b'a bb c '])
358 self.assertEqual(self.type2test(b' a bb c ').split(None, 1), [b'a', b'bb c '])
359 self.assertEqual(self.type2test(b' a bb c ').split(None, 2), [b'a', b'bb', b'c '])
360 self.assertEqual(self.type2test(b' a bb c ').split(None, 3), [b'a', b'bb', b'c'])
361
362 def test_split_string_error(self):
363 self.assertRaises(TypeError, self.type2test(b'a b').split, ' ')
364
Antoine Pitrouf8453022010-01-12 22:05:42 +0000365 def test_split_unicodewhitespace(self):
366 b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F")
367 self.assertEqual(b.split(), [b'\x1c\x1d\x1e\x1f'])
368
Christian Heimes510711d2008-01-30 11:57:58 +0000369 def test_rsplit(self):
370 b = self.type2test(b'mississippi')
371 self.assertEqual(b.rsplit(b'i'), [b'm', b'ss', b'ss', b'pp', b''])
372 self.assertEqual(b.rsplit(b'ss'), [b'mi', b'i', b'ippi'])
373 self.assertEqual(b.rsplit(b'w'), [b])
374
375 def test_rsplit_whitespace(self):
376 for b in (b' arf barf ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf',
377 b'arf\fbarf', b'arf\vbarf'):
378 b = self.type2test(b)
379 self.assertEqual(b.rsplit(), [b'arf', b'barf'])
380 self.assertEqual(b.rsplit(None), [b'arf', b'barf'])
381 self.assertEqual(b.rsplit(None, 2), [b'arf', b'barf'])
382 self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 0), [b' a bb c'])
383 self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 1), [b' a bb', b'c'])
384 self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 2), [b' a', b'bb', b'c'])
385 self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 3), [b'a', b'bb', b'c'])
386
387 def test_rsplit_string_error(self):
388 self.assertRaises(TypeError, self.type2test(b'a b').rsplit, ' ')
389
390 def test_rsplit_unicodewhitespace(self):
391 b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F")
Christian Heimes510711d2008-01-30 11:57:58 +0000392 self.assertEqual(b.rsplit(), [b'\x1c\x1d\x1e\x1f'])
393
394 def test_partition(self):
395 b = self.type2test(b'mississippi')
396 self.assertEqual(b.partition(b'ss'), (b'mi', b'ss', b'issippi'))
Antoine Pitrouf8453022010-01-12 22:05:42 +0000397 self.assertEqual(b.partition(b'w'), (b'mississippi', b'', b''))
Christian Heimes510711d2008-01-30 11:57:58 +0000398
399 def test_rpartition(self):
400 b = self.type2test(b'mississippi')
401 self.assertEqual(b.rpartition(b'ss'), (b'missi', b'ss', b'ippi'))
402 self.assertEqual(b.rpartition(b'i'), (b'mississipp', b'i', b''))
Antoine Pitrouf8453022010-01-12 22:05:42 +0000403 self.assertEqual(b.rpartition(b'w'), (b'', b'', b'mississippi'))
Christian Heimes510711d2008-01-30 11:57:58 +0000404
405 def test_pickling(self):
Hirokazu Yamamotod0d0b652008-10-23 00:38:15 +0000406 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
Christian Heimes510711d2008-01-30 11:57:58 +0000407 for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0":
408 b = self.type2test(b)
409 ps = pickle.dumps(b, proto)
410 q = pickle.loads(ps)
411 self.assertEqual(b, q)
412
413 def test_strip(self):
414 b = self.type2test(b'mississippi')
415 self.assertEqual(b.strip(b'i'), b'mississipp')
416 self.assertEqual(b.strip(b'm'), b'ississippi')
417 self.assertEqual(b.strip(b'pi'), b'mississ')
418 self.assertEqual(b.strip(b'im'), b'ssissipp')
419 self.assertEqual(b.strip(b'pim'), b'ssiss')
420 self.assertEqual(b.strip(b), b'')
421
422 def test_lstrip(self):
423 b = self.type2test(b'mississippi')
424 self.assertEqual(b.lstrip(b'i'), b'mississippi')
425 self.assertEqual(b.lstrip(b'm'), b'ississippi')
426 self.assertEqual(b.lstrip(b'pi'), b'mississippi')
427 self.assertEqual(b.lstrip(b'im'), b'ssissippi')
428 self.assertEqual(b.lstrip(b'pim'), b'ssissippi')
429
430 def test_rstrip(self):
431 b = self.type2test(b'mississippi')
432 self.assertEqual(b.rstrip(b'i'), b'mississipp')
433 self.assertEqual(b.rstrip(b'm'), b'mississippi')
434 self.assertEqual(b.rstrip(b'pi'), b'mississ')
435 self.assertEqual(b.rstrip(b'im'), b'mississipp')
436 self.assertEqual(b.rstrip(b'pim'), b'mississ')
437
438 def test_strip_whitespace(self):
439 b = self.type2test(b' \t\n\r\f\vabc \t\n\r\f\v')
440 self.assertEqual(b.strip(), b'abc')
441 self.assertEqual(b.lstrip(), b'abc \t\n\r\f\v')
442 self.assertEqual(b.rstrip(), b' \t\n\r\f\vabc')
443
444 def test_strip_bytearray(self):
445 self.assertEqual(self.type2test(b'abc').strip(memoryview(b'ac')), b'b')
446 self.assertEqual(self.type2test(b'abc').lstrip(memoryview(b'ac')), b'bc')
447 self.assertEqual(self.type2test(b'abc').rstrip(memoryview(b'ac')), b'ab')
448
449 def test_strip_string_error(self):
450 self.assertRaises(TypeError, self.type2test(b'abc').strip, 'b')
451 self.assertRaises(TypeError, self.type2test(b'abc').lstrip, 'b')
452 self.assertRaises(TypeError, self.type2test(b'abc').rstrip, 'b')
453
454 def test_ord(self):
455 b = self.type2test(b'\0A\x7f\x80\xff')
456 self.assertEqual([ord(b[i:i+1]) for i in range(len(b))],
457 [0, 65, 127, 128, 255])
458
Georg Brandlabc38772009-04-12 15:51:51 +0000459 def test_maketrans(self):
460 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'
Georg Brandlabc38772009-04-12 15:51:51 +0000461 self.assertEqual(self.type2test.maketrans(b'abc', b'xyz'), transtable)
Antoine Pitrou403ce782009-10-14 17:14:16 +0000462 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'
463 self.assertEqual(self.type2test.maketrans(b'\375\376\377', b'xyz'), transtable)
Georg Brandlabc38772009-04-12 15:51:51 +0000464 self.assertRaises(ValueError, self.type2test.maketrans, b'abc', b'xyzq')
465 self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 'def')
466
Christian Heimes510711d2008-01-30 11:57:58 +0000467
468class BytesTest(BaseBytesTest):
469 type2test = bytes
470
Antoine Pitrou2f89aa62008-08-02 21:02:48 +0000471 def test_buffer_is_readonly(self):
Antoine Pitrou47d305d2008-08-16 23:28:44 +0000472 fd = os.dup(sys.stdin.fileno())
473 with open(fd, "rb", buffering=0) as f:
Antoine Pitrou2f89aa62008-08-02 21:02:48 +0000474 self.assertRaises(TypeError, f.readinto, b"")
475
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000476 def test_custom(self):
477 class A:
478 def __bytes__(self):
479 return b'abc'
480 self.assertEqual(bytes(A()), b'abc')
481 class A: pass
482 self.assertRaises(TypeError, bytes, A())
483 class A:
484 def __bytes__(self):
485 return None
486 self.assertRaises(TypeError, bytes, A())
487
Antoine Pitrou2f89aa62008-08-02 21:02:48 +0000488
Christian Heimes510711d2008-01-30 11:57:58 +0000489class ByteArrayTest(BaseBytesTest):
490 type2test = bytearray
Guido van Rossumebea9be2007-04-09 00:49:13 +0000491
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000492 def test_nohash(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000493 self.assertRaises(TypeError, hash, bytearray())
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000494
Guido van Rossum254348e2007-11-21 19:29:53 +0000495 def test_bytearray_api(self):
Guido van Rossum75d6f1a2007-05-24 17:52:00 +0000496 short_sample = b"Hello world\n"
Guido van Rossum98297ee2007-11-06 21:34:58 +0000497 sample = short_sample + b"\0"*(20 - len(short_sample))
Guido van Rossumd624f182006-04-24 13:47:05 +0000498 tfn = tempfile.mktemp()
499 try:
500 # Prepare
501 with open(tfn, "wb") as f:
502 f.write(short_sample)
503 # Test readinto
504 with open(tfn, "rb") as f:
Guido van Rossum254348e2007-11-21 19:29:53 +0000505 b = bytearray(20)
Guido van Rossumd624f182006-04-24 13:47:05 +0000506 n = f.readinto(b)
507 self.assertEqual(n, len(short_sample))
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000508 self.assertEqual(list(b), list(sample))
Guido van Rossumd624f182006-04-24 13:47:05 +0000509 # Test writing in binary mode
510 with open(tfn, "wb") as f:
511 f.write(b)
512 with open(tfn, "rb") as f:
513 self.assertEqual(f.read(), sample)
Guido van Rossum13633bb2007-04-13 18:42:35 +0000514 # Text mode is ambiguous; don't test
Guido van Rossumd624f182006-04-24 13:47:05 +0000515 finally:
516 try:
517 os.remove(tfn)
518 except os.error:
519 pass
520
Neal Norwitz6968b052007-02-27 19:02:19 +0000521 def test_reverse(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000522 b = bytearray(b'hello')
Neal Norwitz6968b052007-02-27 19:02:19 +0000523 self.assertEqual(b.reverse(), None)
524 self.assertEqual(b, b'olleh')
Guido van Rossum254348e2007-11-21 19:29:53 +0000525 b = bytearray(b'hello1') # test even number of items
Neal Norwitz6968b052007-02-27 19:02:19 +0000526 b.reverse()
527 self.assertEqual(b, b'1olleh')
Guido van Rossum254348e2007-11-21 19:29:53 +0000528 b = bytearray()
Neal Norwitz6968b052007-02-27 19:02:19 +0000529 b.reverse()
530 self.assertFalse(b)
531
Guido van Rossumd624f182006-04-24 13:47:05 +0000532 def test_regexps(self):
533 def by(s):
Guido van Rossum254348e2007-11-21 19:29:53 +0000534 return bytearray(map(ord, s))
Guido van Rossumd624f182006-04-24 13:47:05 +0000535 b = by("Hello, world")
Antoine Pitroufd036452008-08-19 17:56:33 +0000536 self.assertEqual(re.findall(br"\w+", b), [by("Hello"), by("world")])
Guido van Rossumd624f182006-04-24 13:47:05 +0000537
538 def test_setitem(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000539 b = bytearray([1, 2, 3])
Guido van Rossumd624f182006-04-24 13:47:05 +0000540 b[1] = 100
Guido van Rossum254348e2007-11-21 19:29:53 +0000541 self.assertEqual(b, bytearray([1, 100, 3]))
Guido van Rossumd624f182006-04-24 13:47:05 +0000542 b[-1] = 200
Guido van Rossum254348e2007-11-21 19:29:53 +0000543 self.assertEqual(b, bytearray([1, 100, 200]))
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000544 b[0] = Indexable(10)
Guido van Rossum254348e2007-11-21 19:29:53 +0000545 self.assertEqual(b, bytearray([10, 100, 200]))
Guido van Rossumd624f182006-04-24 13:47:05 +0000546 try:
547 b[3] = 0
548 self.fail("Didn't raise IndexError")
549 except IndexError:
550 pass
551 try:
552 b[-10] = 0
553 self.fail("Didn't raise IndexError")
554 except IndexError:
555 pass
556 try:
557 b[0] = 256
558 self.fail("Didn't raise ValueError")
559 except ValueError:
560 pass
561 try:
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000562 b[0] = Indexable(-1)
Guido van Rossumd624f182006-04-24 13:47:05 +0000563 self.fail("Didn't raise ValueError")
564 except ValueError:
565 pass
566 try:
567 b[0] = None
568 self.fail("Didn't raise TypeError")
569 except TypeError:
570 pass
571
572 def test_delitem(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000573 b = bytearray(range(10))
Guido van Rossumd624f182006-04-24 13:47:05 +0000574 del b[0]
Guido van Rossum254348e2007-11-21 19:29:53 +0000575 self.assertEqual(b, bytearray(range(1, 10)))
Guido van Rossumd624f182006-04-24 13:47:05 +0000576 del b[-1]
Guido van Rossum254348e2007-11-21 19:29:53 +0000577 self.assertEqual(b, bytearray(range(1, 9)))
Guido van Rossumd624f182006-04-24 13:47:05 +0000578 del b[4]
Guido van Rossum254348e2007-11-21 19:29:53 +0000579 self.assertEqual(b, bytearray([1, 2, 3, 4, 6, 7, 8]))
Guido van Rossumd624f182006-04-24 13:47:05 +0000580
581 def test_setslice(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000582 b = bytearray(range(10))
Guido van Rossumd624f182006-04-24 13:47:05 +0000583 self.assertEqual(list(b), list(range(10)))
584
Guido van Rossum254348e2007-11-21 19:29:53 +0000585 b[0:5] = bytearray([1, 1, 1, 1, 1])
586 self.assertEqual(b, bytearray([1, 1, 1, 1, 1, 5, 6, 7, 8, 9]))
Guido van Rossumd624f182006-04-24 13:47:05 +0000587
588 del b[0:-5]
Guido van Rossum254348e2007-11-21 19:29:53 +0000589 self.assertEqual(b, bytearray([5, 6, 7, 8, 9]))
Guido van Rossumd624f182006-04-24 13:47:05 +0000590
Guido van Rossum254348e2007-11-21 19:29:53 +0000591 b[0:0] = bytearray([0, 1, 2, 3, 4])
592 self.assertEqual(b, bytearray(range(10)))
Guido van Rossumd624f182006-04-24 13:47:05 +0000593
Guido van Rossum254348e2007-11-21 19:29:53 +0000594 b[-7:-3] = bytearray([100, 101])
595 self.assertEqual(b, bytearray([0, 1, 2, 100, 101, 7, 8, 9]))
Guido van Rossumd624f182006-04-24 13:47:05 +0000596
597 b[3:5] = [3, 4, 5, 6]
Guido van Rossum254348e2007-11-21 19:29:53 +0000598 self.assertEqual(b, bytearray(range(10)))
Guido van Rossumad7d8d12007-04-13 01:39:34 +0000599
Thomas Wouters9a6e62b2006-08-23 23:20:29 +0000600 b[3:0] = [42, 42, 42]
Guido van Rossum254348e2007-11-21 19:29:53 +0000601 self.assertEqual(b, bytearray([0, 1, 2, 42, 42, 42, 3, 4, 5, 6, 7, 8, 9]))
Guido van Rossumd624f182006-04-24 13:47:05 +0000602
Thomas Wouters376446d2006-12-19 08:30:14 +0000603 def test_extended_set_del_slice(self):
604 indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
605 for start in indices:
606 for stop in indices:
607 # Skip invalid step 0
608 for step in indices[1:]:
609 L = list(range(255))
Guido van Rossum254348e2007-11-21 19:29:53 +0000610 b = bytearray(L)
Thomas Wouters376446d2006-12-19 08:30:14 +0000611 # Make sure we have a slice of exactly the right length,
612 # but with different data.
613 data = L[start:stop:step]
614 data.reverse()
615 L[start:stop:step] = data
616 b[start:stop:step] = data
Guido van Rossum254348e2007-11-21 19:29:53 +0000617 self.assertEquals(b, bytearray(L))
Guido van Rossumad7d8d12007-04-13 01:39:34 +0000618
Thomas Wouters376446d2006-12-19 08:30:14 +0000619 del L[start:stop:step]
620 del b[start:stop:step]
Guido van Rossum254348e2007-11-21 19:29:53 +0000621 self.assertEquals(b, bytearray(L))
Thomas Wouters376446d2006-12-19 08:30:14 +0000622
Guido van Rossumd624f182006-04-24 13:47:05 +0000623 def test_setslice_trap(self):
624 # This test verifies that we correctly handle assigning self
625 # to a slice of self (the old Lambert Meertens trap).
Guido van Rossum254348e2007-11-21 19:29:53 +0000626 b = bytearray(range(256))
Guido van Rossumd624f182006-04-24 13:47:05 +0000627 b[8:] = b
Guido van Rossum254348e2007-11-21 19:29:53 +0000628 self.assertEqual(b, bytearray(list(range(8)) + list(range(256))))
Guido van Rossumd624f182006-04-24 13:47:05 +0000629
Guido van Rossum13e57212006-04-27 22:54:26 +0000630 def test_iconcat(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000631 b = bytearray(b"abc")
Guido van Rossum13e57212006-04-27 22:54:26 +0000632 b1 = b
Guido van Rossum9c627722007-08-27 18:31:48 +0000633 b += b"def"
634 self.assertEqual(b, b"abcdef")
Guido van Rossum13e57212006-04-27 22:54:26 +0000635 self.assertEqual(b, b1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000636 self.assertTrue(b is b1)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000637 b += b"xyz"
Guido van Rossumad7d8d12007-04-13 01:39:34 +0000638 self.assertEqual(b, b"abcdefxyz")
639 try:
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000640 b += ""
Guido van Rossumad7d8d12007-04-13 01:39:34 +0000641 except TypeError:
642 pass
643 else:
644 self.fail("bytes += unicode didn't raise TypeError")
Guido van Rossum13e57212006-04-27 22:54:26 +0000645
646 def test_irepeat(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000647 b = bytearray(b"abc")
Guido van Rossum13e57212006-04-27 22:54:26 +0000648 b1 = b
649 b *= 3
Guido van Rossum9c627722007-08-27 18:31:48 +0000650 self.assertEqual(b, b"abcabcabc")
Guido van Rossum13e57212006-04-27 22:54:26 +0000651 self.assertEqual(b, b1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000652 self.assertTrue(b is b1)
Guido van Rossum13e57212006-04-27 22:54:26 +0000653
654 def test_irepeat_1char(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000655 b = bytearray(b"x")
Guido van Rossum13e57212006-04-27 22:54:26 +0000656 b1 = b
657 b *= 100
Guido van Rossum98297ee2007-11-06 21:34:58 +0000658 self.assertEqual(b, b"x"*100)
Guido van Rossum13e57212006-04-27 22:54:26 +0000659 self.assertEqual(b, b1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000660 self.assertTrue(b is b1)
Guido van Rossum13e57212006-04-27 22:54:26 +0000661
Guido van Rossum20188312006-05-05 15:15:40 +0000662 def test_alloc(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000663 b = bytearray()
Guido van Rossum20188312006-05-05 15:15:40 +0000664 alloc = b.__alloc__()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000665 self.assertTrue(alloc >= 0)
Guido van Rossum20188312006-05-05 15:15:40 +0000666 seq = [alloc]
667 for i in range(100):
Guido van Rossum9c627722007-08-27 18:31:48 +0000668 b += b"x"
Guido van Rossum20188312006-05-05 15:15:40 +0000669 alloc = b.__alloc__()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000670 self.assertTrue(alloc >= len(b))
Guido van Rossum20188312006-05-05 15:15:40 +0000671 if alloc not in seq:
672 seq.append(alloc)
Guido van Rossum20188312006-05-05 15:15:40 +0000673
Neal Norwitz6968b052007-02-27 19:02:19 +0000674 def test_extend(self):
675 orig = b'hello'
Guido van Rossum254348e2007-11-21 19:29:53 +0000676 a = bytearray(orig)
Neal Norwitz6968b052007-02-27 19:02:19 +0000677 a.extend(a)
678 self.assertEqual(a, orig + orig)
679 self.assertEqual(a[5:], orig)
Alexandre Vassalotti09121e82007-12-04 05:51:13 +0000680 a = bytearray(b'')
681 # Test iterators that don't have a __length_hint__
682 a.extend(map(int, orig * 25))
683 a.extend(int(x) for x in orig * 25)
684 self.assertEqual(a, orig * 50)
685 self.assertEqual(a[-5:], orig)
686 a = bytearray(b'')
687 a.extend(iter(map(int, orig * 50)))
688 self.assertEqual(a, orig * 50)
689 self.assertEqual(a[-5:], orig)
690 a = bytearray(b'')
691 a.extend(list(map(int, orig * 50)))
692 self.assertEqual(a, orig * 50)
693 self.assertEqual(a[-5:], orig)
694 a = bytearray(b'')
695 self.assertRaises(ValueError, a.extend, [0, 1, 2, 256])
696 self.assertRaises(ValueError, a.extend, [0, 1, 2, -1])
697 self.assertEqual(len(a), 0)
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000698 a = bytearray(b'')
699 a.extend([Indexable(ord('a'))])
700 self.assertEqual(a, b'a')
Guido van Rossumad7d8d12007-04-13 01:39:34 +0000701
Neal Norwitz6968b052007-02-27 19:02:19 +0000702 def test_remove(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000703 b = bytearray(b'hello')
Neal Norwitz6968b052007-02-27 19:02:19 +0000704 b.remove(ord('l'))
705 self.assertEqual(b, b'helo')
706 b.remove(ord('l'))
707 self.assertEqual(b, b'heo')
708 self.assertRaises(ValueError, lambda: b.remove(ord('l')))
709 self.assertRaises(ValueError, lambda: b.remove(400))
Gregory P. Smith60d241f2007-10-16 06:31:30 +0000710 self.assertRaises(TypeError, lambda: b.remove('e'))
Neal Norwitz6968b052007-02-27 19:02:19 +0000711 # remove first and last
712 b.remove(ord('o'))
713 b.remove(ord('h'))
714 self.assertEqual(b, b'e')
Gregory P. Smith60d241f2007-10-16 06:31:30 +0000715 self.assertRaises(TypeError, lambda: b.remove(b'e'))
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000716 b.remove(Indexable(ord('e')))
717 self.assertEqual(b, b'')
Neal Norwitz6968b052007-02-27 19:02:19 +0000718
719 def test_pop(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000720 b = bytearray(b'world')
Neal Norwitz6968b052007-02-27 19:02:19 +0000721 self.assertEqual(b.pop(), ord('d'))
722 self.assertEqual(b.pop(0), ord('w'))
723 self.assertEqual(b.pop(-2), ord('r'))
724 self.assertRaises(IndexError, lambda: b.pop(10))
Guido van Rossum254348e2007-11-21 19:29:53 +0000725 self.assertRaises(OverflowError, lambda: bytearray().pop())
Mark Dickinson54a3db92009-09-06 10:19:23 +0000726 # test for issue #6846
727 self.assertEqual(bytearray(b'\xff').pop(), 0xff)
Neal Norwitz6968b052007-02-27 19:02:19 +0000728
729 def test_nosort(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000730 self.assertRaises(AttributeError, lambda: bytearray().sort())
Neal Norwitz6968b052007-02-27 19:02:19 +0000731
Neal Norwitz6968b052007-02-27 19:02:19 +0000732 def test_append(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000733 b = bytearray(b'hell')
Neal Norwitz6968b052007-02-27 19:02:19 +0000734 b.append(ord('o'))
735 self.assertEqual(b, b'hello')
736 self.assertEqual(b.append(100), None)
Guido van Rossum254348e2007-11-21 19:29:53 +0000737 b = bytearray()
Neal Norwitz6968b052007-02-27 19:02:19 +0000738 b.append(ord('A'))
739 self.assertEqual(len(b), 1)
Gregory P. Smith60d241f2007-10-16 06:31:30 +0000740 self.assertRaises(TypeError, lambda: b.append(b'o'))
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000741 b = bytearray()
742 b.append(Indexable(ord('A')))
743 self.assertEqual(b, b'A')
Neal Norwitz6968b052007-02-27 19:02:19 +0000744
745 def test_insert(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000746 b = bytearray(b'msssspp')
Neal Norwitz6968b052007-02-27 19:02:19 +0000747 b.insert(1, ord('i'))
748 b.insert(4, ord('i'))
749 b.insert(-2, ord('i'))
750 b.insert(1000, ord('i'))
751 self.assertEqual(b, b'mississippi')
Gregory P. Smith60d241f2007-10-16 06:31:30 +0000752 self.assertRaises(TypeError, lambda: b.insert(0, b'1'))
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000753 b = bytearray()
754 b.insert(0, Indexable(ord('A')))
755 self.assertEqual(b, b'A')
Neal Norwitz6968b052007-02-27 19:02:19 +0000756
Benjamin Peterson0f3641c2008-11-19 22:05:52 +0000757 def test_copied(self):
758 # Issue 4348. Make sure that operations that don't mutate the array
759 # copy the bytes.
760 b = bytearray(b'abc')
Amaury Forgeot d'Arcb0c29162008-11-22 22:18:04 +0000761 self.assertFalse(b is b.replace(b'abc', b'cde', 0))
Benjamin Peterson0f3641c2008-11-19 22:05:52 +0000762
763 t = bytearray([i for i in range(256)])
764 x = bytearray(b'')
765 self.assertFalse(x is x.translate(t))
766
Guido van Rossum254348e2007-11-21 19:29:53 +0000767 def test_partition_bytearray_doesnt_share_nullstring(self):
768 a, b, c = bytearray(b"x").partition(b"y")
Guido van Rossum98297ee2007-11-06 21:34:58 +0000769 self.assertEqual(b, b"")
770 self.assertEqual(c, b"")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000771 self.assertTrue(b is not c)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000772 b += b"!"
773 self.assertEqual(c, b"")
Guido van Rossum254348e2007-11-21 19:29:53 +0000774 a, b, c = bytearray(b"x").partition(b"y")
Guido van Rossum98297ee2007-11-06 21:34:58 +0000775 self.assertEqual(b, b"")
776 self.assertEqual(c, b"")
777 # Same for rpartition
Guido van Rossum254348e2007-11-21 19:29:53 +0000778 b, c, a = bytearray(b"x").rpartition(b"y")
Guido van Rossum98297ee2007-11-06 21:34:58 +0000779 self.assertEqual(b, b"")
780 self.assertEqual(c, b"")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000781 self.assertTrue(b is not c)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000782 b += b"!"
783 self.assertEqual(c, b"")
Guido van Rossum254348e2007-11-21 19:29:53 +0000784 c, b, a = bytearray(b"x").rpartition(b"y")
Guido van Rossum98297ee2007-11-06 21:34:58 +0000785 self.assertEqual(b, b"")
786 self.assertEqual(c, b"")
787
Antoine Pitrou5504e892008-12-06 21:27:53 +0000788 def test_resize_forbidden(self):
789 # #4509: can't resize a bytearray when there are buffer exports, even
790 # if it wouldn't reallocate the underlying buffer.
791 # Furthermore, no destructive changes to the buffer may be applied
792 # before raising the error.
793 b = bytearray(range(10))
794 v = memoryview(b)
795 def resize(n):
796 b[1:-1] = range(n + 1, 2*n - 1)
797 resize(10)
798 orig = b[:]
799 self.assertRaises(BufferError, resize, 11)
800 self.assertEquals(b, orig)
801 self.assertRaises(BufferError, resize, 9)
802 self.assertEquals(b, orig)
803 self.assertRaises(BufferError, resize, 0)
804 self.assertEquals(b, orig)
805 # Other operations implying resize
806 self.assertRaises(BufferError, b.pop, 0)
807 self.assertEquals(b, orig)
808 self.assertRaises(BufferError, b.remove, b[1])
809 self.assertEquals(b, orig)
810 def delitem():
811 del b[1]
812 self.assertRaises(BufferError, delitem)
813 self.assertEquals(b, orig)
814 # deleting a non-contiguous slice
815 def delslice():
816 b[1:-1:2] = b""
817 self.assertRaises(BufferError, delslice)
818 self.assertEquals(b, orig)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000819
Antoine Pitroufc8d6f42010-01-17 12:38:54 +0000820 def test_empty_bytearray(self):
821 # Issue #7561: operations on empty bytearrays could crash in many
822 # situations, due to a fragile implementation of the
823 # PyByteArray_AS_STRING() C macro.
824 self.assertRaises(ValueError, int, bytearray(b''))
825 self.assertRaises((ValueError, OSError), os.mkdir, bytearray(b''))
826
827
Christian Heimes510711d2008-01-30 11:57:58 +0000828class AssortedBytesTest(unittest.TestCase):
829 #
830 # Test various combinations of bytes and bytearray
831 #
832
833 def setUp(self):
834 self.warning_filters = warnings.filters[:]
835
836 def tearDown(self):
837 warnings.filters = self.warning_filters
838
839 def test_repr_str(self):
840 warnings.simplefilter('ignore', BytesWarning)
841 for f in str, repr:
842 self.assertEqual(f(bytearray()), "bytearray(b'')")
843 self.assertEqual(f(bytearray([0])), "bytearray(b'\\x00')")
844 self.assertEqual(f(bytearray([0, 1, 254, 255])),
845 "bytearray(b'\\x00\\x01\\xfe\\xff')")
846 self.assertEqual(f(b"abc"), "b'abc'")
847 self.assertEqual(f(b"'"), '''b"'"''') # '''
848 self.assertEqual(f(b"'\""), r"""b'\'"'""") # '
849
850 def test_compare_bytes_to_bytearray(self):
851 self.assertEqual(b"abc" == bytes(b"abc"), True)
852 self.assertEqual(b"ab" != bytes(b"abc"), True)
853 self.assertEqual(b"ab" <= bytes(b"abc"), True)
854 self.assertEqual(b"ab" < bytes(b"abc"), True)
855 self.assertEqual(b"abc" >= bytes(b"ab"), True)
856 self.assertEqual(b"abc" > bytes(b"ab"), True)
857
858 self.assertEqual(b"abc" != bytes(b"abc"), False)
859 self.assertEqual(b"ab" == bytes(b"abc"), False)
860 self.assertEqual(b"ab" > bytes(b"abc"), False)
861 self.assertEqual(b"ab" >= bytes(b"abc"), False)
862 self.assertEqual(b"abc" < bytes(b"ab"), False)
863 self.assertEqual(b"abc" <= bytes(b"ab"), False)
864
865 self.assertEqual(bytes(b"abc") == b"abc", True)
866 self.assertEqual(bytes(b"ab") != b"abc", True)
867 self.assertEqual(bytes(b"ab") <= b"abc", True)
868 self.assertEqual(bytes(b"ab") < b"abc", True)
869 self.assertEqual(bytes(b"abc") >= b"ab", True)
870 self.assertEqual(bytes(b"abc") > b"ab", True)
871
872 self.assertEqual(bytes(b"abc") != b"abc", False)
873 self.assertEqual(bytes(b"ab") == b"abc", False)
874 self.assertEqual(bytes(b"ab") > b"abc", False)
875 self.assertEqual(bytes(b"ab") >= b"abc", False)
876 self.assertEqual(bytes(b"abc") < b"ab", False)
877 self.assertEqual(bytes(b"abc") <= b"ab", False)
878
879 def test_doc(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000880 self.assertTrue(bytearray.__doc__ != None)
881 self.assertTrue(bytearray.__doc__.startswith("bytearray("), bytearray.__doc__)
882 self.assertTrue(bytes.__doc__ != None)
883 self.assertTrue(bytes.__doc__.startswith("bytes("), bytes.__doc__)
Christian Heimes510711d2008-01-30 11:57:58 +0000884
885 def test_from_bytearray(self):
886 sample = bytes(b"Hello world\n\x80\x81\xfe\xff")
887 buf = memoryview(sample)
888 b = bytearray(buf)
889 self.assertEqual(b, bytearray(sample))
890
891 def test_to_str(self):
892 warnings.simplefilter('ignore', BytesWarning)
893 self.assertEqual(str(b''), "b''")
894 self.assertEqual(str(b'x'), "b'x'")
895 self.assertEqual(str(b'\x80'), "b'\\x80'")
896 self.assertEqual(str(bytearray(b'')), "bytearray(b'')")
897 self.assertEqual(str(bytearray(b'x')), "bytearray(b'x')")
898 self.assertEqual(str(bytearray(b'\x80')), "bytearray(b'\\x80')")
899
900 def test_literal(self):
901 tests = [
902 (b"Wonderful spam", "Wonderful spam"),
903 (br"Wonderful spam too", "Wonderful spam too"),
904 (b"\xaa\x00\000\200", "\xaa\x00\000\200"),
905 (br"\xaa\x00\000\200", r"\xaa\x00\000\200"),
906 ]
907 for b, s in tests:
908 self.assertEqual(b, bytearray(s, 'latin-1'))
909 for c in range(128, 256):
910 self.assertRaises(SyntaxError, eval,
911 'b"%s"' % chr(c))
912
913 def test_translate(self):
914 b = b'hello'
Georg Brandlccc47b62008-12-28 11:44:14 +0000915 ba = bytearray(b)
Christian Heimes510711d2008-01-30 11:57:58 +0000916 rosetta = bytearray(range(0, 256))
917 rosetta[ord('o')] = ord('e')
918 c = b.translate(rosetta, b'l')
919 self.assertEqual(b, b'hello')
920 self.assertEqual(c, b'hee')
Georg Brandlccc47b62008-12-28 11:44:14 +0000921 c = ba.translate(rosetta, b'l')
922 self.assertEqual(ba, b'hello')
923 self.assertEqual(c, b'hee')
924 c = b.translate(None, b'e')
925 self.assertEqual(c, b'hllo')
926 c = ba.translate(None, b'e')
927 self.assertEqual(c, b'hllo')
928 self.assertRaises(TypeError, b.translate, None, None)
929 self.assertRaises(TypeError, ba.translate, None, None)
Christian Heimes510711d2008-01-30 11:57:58 +0000930
931 def test_split_bytearray(self):
932 self.assertEqual(b'a b'.split(memoryview(b' ')), [b'a', b'b'])
933
934 def test_rsplit_bytearray(self):
935 self.assertEqual(b'a b'.rsplit(memoryview(b' ')), [b'a', b'b'])
936
Georg Brandleb2c9642008-05-30 12:05:02 +0000937 def test_return_self(self):
938 # bytearray.replace must always return a new bytearray
939 b = bytearray()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000940 self.assertFalse(b.replace(b'', b'') is b)
Georg Brandleb2c9642008-05-30 12:05:02 +0000941
Barry Warsaw9e9dcd62008-10-17 01:50:37 +0000942 def test_compare(self):
943 if sys.flags.bytes_warning:
944 warnings.simplefilter('error', BytesWarning)
945 self.assertRaises(BytesWarning, operator.eq, b'', '')
946 self.assertRaises(BytesWarning, operator.ne, b'', '')
947 self.assertRaises(BytesWarning, operator.eq, bytearray(b''), '')
948 self.assertRaises(BytesWarning, operator.ne, bytearray(b''), '')
949 else:
Antoine Pitrouc9e92162009-11-01 15:59:11 +0000950 # self.skipTest("BytesWarning is needed for this test: use -bb option")
Barry Warsaw9e9dcd62008-10-17 01:50:37 +0000951 pass
952
Guido van Rossumd624f182006-04-24 13:47:05 +0000953 # Optimizations:
Guido van Rossume06b6b82006-04-23 07:43:54 +0000954 # __iter__? (optimization)
Guido van Rossumd624f182006-04-24 13:47:05 +0000955 # __reversed__? (optimization)
956
Guido van Rossumad7d8d12007-04-13 01:39:34 +0000957 # XXX More string methods? (Those that don't use character properties)
958
Neal Norwitz6968b052007-02-27 19:02:19 +0000959 # There are tests in string_tests.py that are more
960 # comprehensive for things like split, partition, etc.
961 # Unfortunately they are all bundled with tests that
962 # are not appropriate for bytes
Guido van Rossume06b6b82006-04-23 07:43:54 +0000963
Guido van Rossum254348e2007-11-21 19:29:53 +0000964 # I've started porting some of those into bytearray_tests.py, we should port
Gregory P. Smith60d241f2007-10-16 06:31:30 +0000965 # the rest that make sense (the code can be cleaned up to use modern
966 # unittest methods at the same time).
967
Guido van Rossum254348e2007-11-21 19:29:53 +0000968class BytearrayPEP3137Test(unittest.TestCase,
Gregory P. Smith60d241f2007-10-16 06:31:30 +0000969 test.buffer_tests.MixinBytesBufferCommonTests):
970 def marshal(self, x):
Guido van Rossum254348e2007-11-21 19:29:53 +0000971 return bytearray(x)
Gregory P. Smith60d241f2007-10-16 06:31:30 +0000972
973 def test_returns_new_copy(self):
974 val = self.marshal(b'1234')
975 # On immutable types these MAY return a reference to themselves
Guido van Rossum254348e2007-11-21 19:29:53 +0000976 # but on mutable types like bytearray they MUST return a new copy.
Gregory P. Smith60d241f2007-10-16 06:31:30 +0000977 for methname in ('zfill', 'rjust', 'ljust', 'center'):
978 method = getattr(val, methname)
979 newval = method(3)
980 self.assertEqual(val, newval)
981 self.assertTrue(val is not newval,
982 methname+' returned self on a mutable object')
Antoine Pitroucbaa62502010-01-13 15:03:39 +0000983 for expr in ('val.split()[0]', 'val.rsplit()[0]',
984 'val.partition(b".")[0]', 'val.rpartition(b".")[2]',
985 'val.splitlines()[0]', 'val.replace(b"", b"")'):
986 newval = eval(expr)
987 self.assertEqual(val, newval)
988 self.assertTrue(val is not newval,
989 expr+' returned val on a mutable object')
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000990
Christian Heimes510711d2008-01-30 11:57:58 +0000991class FixedStringTest(test.string_tests.BaseTest):
Georg Brandlc7885542007-03-06 19:16:20 +0000992
Guido van Rossum9c627722007-08-27 18:31:48 +0000993 def fixtype(self, obj):
994 if isinstance(obj, str):
995 return obj.encode("utf-8")
996 return super().fixtype(obj)
997
Georg Brandlc7885542007-03-06 19:16:20 +0000998 # Currently the bytes containment testing uses a single integer
999 # value. This may not be the final design, but until then the
1000 # bytes section with in a bytes containment not valid
1001 def test_contains(self):
1002 pass
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001003 def test_expandtabs(self):
1004 pass
1005 def test_upper(self):
1006 pass
1007 def test_lower(self):
1008 pass
Georg Brandlc7885542007-03-06 19:16:20 +00001009
Christian Heimes510711d2008-01-30 11:57:58 +00001010class ByteArrayAsStringTest(FixedStringTest):
1011 type2test = bytearray
1012
1013class BytesAsStringTest(FixedStringTest):
1014 type2test = bytes
1015
Georg Brandlc7885542007-03-06 19:16:20 +00001016
Alexandre Vassalotti41f58a72010-01-12 01:23:09 +00001017class SubclassTest(unittest.TestCase):
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001018
1019 def test_basic(self):
Alexandre Vassalotti41f58a72010-01-12 01:23:09 +00001020 self.assertTrue(issubclass(self.subclass2test, self.type2test))
1021 self.assertTrue(isinstance(self.subclass2test(), self.type2test))
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001022
1023 a, b = b"abcd", b"efgh"
Alexandre Vassalotti41f58a72010-01-12 01:23:09 +00001024 _a, _b = self.subclass2test(a), self.subclass2test(b)
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001025
1026 # test comparison operators with subclass instances
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001027 self.assertTrue(_a == _a)
1028 self.assertTrue(_a != _b)
1029 self.assertTrue(_a < _b)
1030 self.assertTrue(_a <= _b)
1031 self.assertTrue(_b >= _a)
1032 self.assertTrue(_b > _a)
1033 self.assertTrue(_a is not a)
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001034
1035 # test concat of subclass instances
1036 self.assertEqual(a + b, _a + _b)
1037 self.assertEqual(a + b, a + _b)
1038 self.assertEqual(a + b, _a + b)
1039
1040 # test repeat
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001041 self.assertTrue(a*5 == _a*5)
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001042
1043 def test_join(self):
1044 # Make sure join returns a NEW object for single item sequences
1045 # involving a subclass.
1046 # Make sure that it is of the appropriate type.
Alexandre Vassalotti41f58a72010-01-12 01:23:09 +00001047 s1 = self.subclass2test(b"abcd")
1048 s2 = self.type2test().join([s1])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001049 self.assertTrue(s1 is not s2)
Alexandre Vassalotti41f58a72010-01-12 01:23:09 +00001050 self.assertTrue(type(s2) is self.type2test, type(s2))
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001051
1052 # Test reverse, calling join on subclass
1053 s3 = s1.join([b"abcd"])
Alexandre Vassalotti41f58a72010-01-12 01:23:09 +00001054 self.assertTrue(type(s3) is self.type2test)
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001055
1056 def test_pickle(self):
Alexandre Vassalotti41f58a72010-01-12 01:23:09 +00001057 a = self.subclass2test(b"abcd")
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001058 a.x = 10
Alexandre Vassalotti41f58a72010-01-12 01:23:09 +00001059 a.y = self.subclass2test(b"efgh")
Hirokazu Yamamotod0d0b652008-10-23 00:38:15 +00001060 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001061 b = pickle.loads(pickle.dumps(a, proto))
1062 self.assertNotEqual(id(a), id(b))
1063 self.assertEqual(a, b)
1064 self.assertEqual(a.x, b.x)
1065 self.assertEqual(a.y, b.y)
1066 self.assertEqual(type(a), type(b))
1067 self.assertEqual(type(a.y), type(b.y))
1068
1069 def test_copy(self):
Alexandre Vassalotti41f58a72010-01-12 01:23:09 +00001070 a = self.subclass2test(b"abcd")
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001071 a.x = 10
Alexandre Vassalotti41f58a72010-01-12 01:23:09 +00001072 a.y = self.subclass2test(b"efgh")
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001073 for copy_method in (copy.copy, copy.deepcopy):
1074 b = copy_method(a)
1075 self.assertNotEqual(id(a), id(b))
1076 self.assertEqual(a, b)
1077 self.assertEqual(a.x, b.x)
1078 self.assertEqual(a.y, b.y)
1079 self.assertEqual(type(a), type(b))
1080 self.assertEqual(type(a.y), type(b.y))
1081
Alexandre Vassalotti41f58a72010-01-12 01:23:09 +00001082
1083class ByteArraySubclass(bytearray):
1084 pass
1085
1086class BytesSubclass(bytes):
1087 pass
1088
1089class ByteArraySubclassTest(SubclassTest):
1090 type2test = bytearray
1091 subclass2test = ByteArraySubclass
1092
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001093 def test_init_override(self):
Guido van Rossum254348e2007-11-21 19:29:53 +00001094 class subclass(bytearray):
Alexandre Vassalotti41f58a72010-01-12 01:23:09 +00001095 def __init__(me, newarg=1, *args, **kwargs):
1096 bytearray.__init__(me, *args, **kwargs)
1097 x = subclass(4, b"abcd")
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001098 x = subclass(4, source=b"abcd")
1099 self.assertEqual(x, b"abcd")
1100 x = subclass(newarg=4, source=b"abcd")
1101 self.assertEqual(x, b"abcd")
1102
1103
Alexandre Vassalotti41f58a72010-01-12 01:23:09 +00001104class BytesSubclassTest(SubclassTest):
1105 type2test = bytes
1106 subclass2test = BytesSubclass
1107
1108
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001109def test_main():
Antoine Pitrou403ce782009-10-14 17:14:16 +00001110 test.support.run_unittest(
1111 BytesTest, AssortedBytesTest, BytesAsStringTest,
Alexandre Vassalotti41f58a72010-01-12 01:23:09 +00001112 ByteArrayTest, ByteArrayAsStringTest, BytesSubclassTest,
1113 ByteArraySubclassTest, BytearrayPEP3137Test)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001114
1115if __name__ == "__main__":
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001116 test_main()