blob: a1e08cc735eff2976a01c15ec97c525acf624c50 [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
41 def test_empty_sequence(self):
Christian Heimes510711d2008-01-30 11:57:58 +000042 b = self.type2test()
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000043 self.assertEqual(len(b), 0)
44 self.assertRaises(IndexError, lambda: b[0])
45 self.assertRaises(IndexError, lambda: b[1])
Christian Heimesa37d4c62007-12-04 23:02:19 +000046 self.assertRaises(IndexError, lambda: b[sys.maxsize])
47 self.assertRaises(IndexError, lambda: b[sys.maxsize+1])
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000048 self.assertRaises(IndexError, lambda: b[10**100])
49 self.assertRaises(IndexError, lambda: b[-1])
50 self.assertRaises(IndexError, lambda: b[-2])
Christian Heimesa37d4c62007-12-04 23:02:19 +000051 self.assertRaises(IndexError, lambda: b[-sys.maxsize])
52 self.assertRaises(IndexError, lambda: b[-sys.maxsize-1])
53 self.assertRaises(IndexError, lambda: b[-sys.maxsize-2])
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000054 self.assertRaises(IndexError, lambda: b[-10**100])
55
56 def test_from_list(self):
57 ints = list(range(256))
Christian Heimes510711d2008-01-30 11:57:58 +000058 b = self.type2test(i for i in ints)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000059 self.assertEqual(len(b), 256)
60 self.assertEqual(list(b), ints)
61
62 def test_from_index(self):
Georg Brandl9a54d7c2008-07-16 23:15:30 +000063 b = self.type2test([Indexable(), Indexable(1), Indexable(254),
64 Indexable(255)])
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000065 self.assertEqual(list(b), [0, 1, 254, 255])
Benjamin Peterson0ff8a502010-04-16 23:19:11 +000066 self.assertRaises(ValueError, self.type2test, [Indexable(-1)])
67 self.assertRaises(ValueError, self.type2test, [Indexable(256)])
Guido van Rossum98297ee2007-11-06 21:34:58 +000068
69 def test_from_ssize(self):
Benjamin Peterson0ff8a502010-04-16 23:19:11 +000070 self.assertEqual(self.type2test(0), b'')
71 self.assertEqual(self.type2test(1), b'\x00')
72 self.assertEqual(self.type2test(5), b'\x00\x00\x00\x00\x00')
73 self.assertRaises(ValueError, self.type2test, -1)
Guido van Rossum98297ee2007-11-06 21:34:58 +000074
Benjamin Peterson0ff8a502010-04-16 23:19:11 +000075 self.assertEqual(self.type2test('0', 'ascii'), b'0')
76 self.assertEqual(self.type2test(b'0'), b'0')
77 self.assertRaises(OverflowError, self.type2test, sys.maxsize + 1)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000078
79 def test_constructor_type_errors(self):
Christian Heimes510711d2008-01-30 11:57:58 +000080 self.assertRaises(TypeError, self.type2test, 0.0)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000081 class C:
82 pass
Christian Heimes510711d2008-01-30 11:57:58 +000083 self.assertRaises(TypeError, self.type2test, ["0"])
84 self.assertRaises(TypeError, self.type2test, [0.0])
85 self.assertRaises(TypeError, self.type2test, [None])
86 self.assertRaises(TypeError, self.type2test, [C()])
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000087
88 def test_constructor_value_errors(self):
Christian Heimes510711d2008-01-30 11:57:58 +000089 self.assertRaises(ValueError, self.type2test, [-1])
90 self.assertRaises(ValueError, self.type2test, [-sys.maxsize])
91 self.assertRaises(ValueError, self.type2test, [-sys.maxsize-1])
92 self.assertRaises(ValueError, self.type2test, [-sys.maxsize-2])
93 self.assertRaises(ValueError, self.type2test, [-10**100])
94 self.assertRaises(ValueError, self.type2test, [256])
95 self.assertRaises(ValueError, self.type2test, [257])
96 self.assertRaises(ValueError, self.type2test, [sys.maxsize])
97 self.assertRaises(ValueError, self.type2test, [sys.maxsize+1])
98 self.assertRaises(ValueError, self.type2test, [10**100])
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000099
100 def test_compare(self):
Christian Heimes510711d2008-01-30 11:57:58 +0000101 b1 = self.type2test([1, 2, 3])
102 b2 = self.type2test([1, 2, 3])
103 b3 = self.type2test([1, 3])
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000104
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000105 self.assertEqual(b1, b2)
Georg Brandlab91fde2009-08-13 08:51:18 +0000106 self.assertTrue(b2 != b3)
107 self.assertTrue(b1 <= b2)
108 self.assertTrue(b1 <= b3)
109 self.assertTrue(b1 < b3)
110 self.assertTrue(b1 >= b2)
111 self.assertTrue(b3 >= b2)
112 self.assertTrue(b3 > b2)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000113
Georg Brandlab91fde2009-08-13 08:51:18 +0000114 self.assertFalse(b1 != b2)
115 self.assertFalse(b2 == b3)
116 self.assertFalse(b1 > b2)
117 self.assertFalse(b1 > b3)
118 self.assertFalse(b1 >= b3)
119 self.assertFalse(b1 < b2)
120 self.assertFalse(b3 < b2)
121 self.assertFalse(b3 <= b2)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000122
Guido van Rossum343e97f2007-04-09 00:43:24 +0000123 def test_compare_to_str(self):
Guido van Rossum98297ee2007-11-06 21:34:58 +0000124 warnings.simplefilter('ignore', BytesWarning)
Guido van Rossum1e35e762007-10-09 17:21:10 +0000125 # Byte comparisons with unicode should always fail!
Guido van Rossumebea9be2007-04-09 00:49:13 +0000126 # Test this for all expected byte orders and Unicode character sizes
Christian Heimes510711d2008-01-30 11:57:58 +0000127 self.assertEqual(self.type2test(b"\0a\0b\0c") == "abc", False)
128 self.assertEqual(self.type2test(b"\0\0\0a\0\0\0b\0\0\0c") == "abc", False)
129 self.assertEqual(self.type2test(b"a\0b\0c\0") == "abc", False)
130 self.assertEqual(self.type2test(b"a\0\0\0b\0\0\0c\0\0\0") == "abc", False)
131 self.assertEqual(self.type2test() == str(), False)
132 self.assertEqual(self.type2test() != str(), True)
133
134 def test_reversed(self):
135 input = list(map(ord, "Hello"))
136 b = self.type2test(input)
137 output = list(reversed(b))
138 input.reverse()
139 self.assertEqual(output, input)
140
141 def test_getslice(self):
142 def by(s):
143 return self.type2test(map(ord, s))
144 b = by("Hello, world")
145
146 self.assertEqual(b[:5], by("Hello"))
147 self.assertEqual(b[1:5], by("ello"))
148 self.assertEqual(b[5:7], by(", "))
149 self.assertEqual(b[7:], by("world"))
150 self.assertEqual(b[7:12], by("world"))
151 self.assertEqual(b[7:100], by("world"))
152
153 self.assertEqual(b[:-7], by("Hello"))
154 self.assertEqual(b[-11:-7], by("ello"))
155 self.assertEqual(b[-7:-5], by(", "))
156 self.assertEqual(b[-5:], by("world"))
157 self.assertEqual(b[-5:12], by("world"))
158 self.assertEqual(b[-5:100], by("world"))
159 self.assertEqual(b[-100:5], by("Hello"))
160
161 def test_extended_getslice(self):
162 # Test extended slicing by comparing with list slicing.
163 L = list(range(255))
164 b = self.type2test(L)
165 indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100)
166 for start in indices:
167 for stop in indices:
168 # Skip step 0 (invalid)
169 for step in indices[1:]:
170 self.assertEqual(b[start:stop:step], self.type2test(L[start:stop:step]))
171
172 def test_encoding(self):
Martin v. Löwisdb12d452009-05-02 18:52:14 +0000173 sample = "Hello world\n\u1234\u5678\u9abc"
Christian Heimes510711d2008-01-30 11:57:58 +0000174 for enc in ("utf8", "utf16"):
175 b = self.type2test(sample, enc)
176 self.assertEqual(b, self.type2test(sample.encode(enc)))
177 self.assertRaises(UnicodeEncodeError, self.type2test, sample, "latin1")
178 b = self.type2test(sample, "latin1", "ignore")
Martin v. Löwisdb12d452009-05-02 18:52:14 +0000179 self.assertEqual(b, self.type2test(sample[:-3], "utf-8"))
Christian Heimes510711d2008-01-30 11:57:58 +0000180
181 def test_decode(self):
182 sample = "Hello world\n\u1234\u5678\u9abc\def0\def0"
183 for enc in ("utf8", "utf16"):
184 b = self.type2test(sample, enc)
185 self.assertEqual(b.decode(enc), sample)
186 sample = "Hello world\n\x80\x81\xfe\xff"
187 b = self.type2test(sample, "latin1")
188 self.assertRaises(UnicodeDecodeError, b.decode, "utf8")
189 self.assertEqual(b.decode("utf8", "ignore"), "Hello world\n")
190
191 def test_from_int(self):
192 b = self.type2test(0)
193 self.assertEqual(b, self.type2test())
194 b = self.type2test(10)
195 self.assertEqual(b, self.type2test([0]*10))
196 b = self.type2test(10000)
197 self.assertEqual(b, self.type2test([0]*10000))
198
199 def test_concat(self):
200 b1 = self.type2test(b"abc")
201 b2 = self.type2test(b"def")
202 self.assertEqual(b1 + b2, b"abcdef")
203 self.assertEqual(b1 + bytes(b"def"), b"abcdef")
204 self.assertEqual(bytes(b"def") + b1, b"defabc")
205 self.assertRaises(TypeError, lambda: b1 + "def")
206 self.assertRaises(TypeError, lambda: "abc" + b2)
207
208 def test_repeat(self):
209 for b in b"abc", self.type2test(b"abc"):
210 self.assertEqual(b * 3, b"abcabcabc")
211 self.assertEqual(b * 0, b"")
212 self.assertEqual(b * -1, b"")
213 self.assertRaises(TypeError, lambda: b * 3.14)
214 self.assertRaises(TypeError, lambda: 3.14 * b)
215 # XXX Shouldn't bytes and bytearray agree on what to raise?
216 self.assertRaises((OverflowError, MemoryError),
217 lambda: b * sys.maxsize)
218
219 def test_repeat_1char(self):
220 self.assertEqual(self.type2test(b'x')*100, self.type2test([ord('x')]*100))
221
222 def test_contains(self):
223 b = self.type2test(b"abc")
Georg Brandlab91fde2009-08-13 08:51:18 +0000224 self.assertTrue(ord('a') in b)
225 self.assertTrue(int(ord('a')) in b)
226 self.assertFalse(200 in b)
227 self.assertFalse(200 in b)
Christian Heimes510711d2008-01-30 11:57:58 +0000228 self.assertRaises(ValueError, lambda: 300 in b)
229 self.assertRaises(ValueError, lambda: -1 in b)
230 self.assertRaises(TypeError, lambda: None in b)
231 self.assertRaises(TypeError, lambda: float(ord('a')) in b)
232 self.assertRaises(TypeError, lambda: "a" in b)
233 for f in bytes, bytearray:
Georg Brandlab91fde2009-08-13 08:51:18 +0000234 self.assertTrue(f(b"") in b)
235 self.assertTrue(f(b"a") in b)
236 self.assertTrue(f(b"b") in b)
237 self.assertTrue(f(b"c") in b)
238 self.assertTrue(f(b"ab") in b)
239 self.assertTrue(f(b"bc") in b)
240 self.assertTrue(f(b"abc") in b)
241 self.assertFalse(f(b"ac") in b)
242 self.assertFalse(f(b"d") in b)
243 self.assertFalse(f(b"dab") in b)
244 self.assertFalse(f(b"abd") in b)
Christian Heimes510711d2008-01-30 11:57:58 +0000245
246 def test_fromhex(self):
247 self.assertRaises(TypeError, self.type2test.fromhex)
248 self.assertRaises(TypeError, self.type2test.fromhex, 1)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000249 self.assertEqual(self.type2test.fromhex(''), self.type2test())
Christian Heimes510711d2008-01-30 11:57:58 +0000250 b = bytearray([0x1a, 0x2b, 0x30])
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000251 self.assertEqual(self.type2test.fromhex('1a2B30'), b)
252 self.assertEqual(self.type2test.fromhex(' 1A 2B 30 '), b)
253 self.assertEqual(self.type2test.fromhex('0000'), b'\0\0')
Christian Heimes510711d2008-01-30 11:57:58 +0000254 self.assertRaises(TypeError, self.type2test.fromhex, b'1B')
255 self.assertRaises(ValueError, self.type2test.fromhex, 'a')
256 self.assertRaises(ValueError, self.type2test.fromhex, 'rt')
257 self.assertRaises(ValueError, self.type2test.fromhex, '1a b cd')
258 self.assertRaises(ValueError, self.type2test.fromhex, '\x00')
259 self.assertRaises(ValueError, self.type2test.fromhex, '12 \x00 34')
260
261 def test_join(self):
262 self.assertEqual(self.type2test(b"").join([]), b"")
263 self.assertEqual(self.type2test(b"").join([b""]), b"")
264 for lst in [[b"abc"], [b"a", b"bc"], [b"ab", b"c"], [b"a", b"b", b"c"]]:
265 lst = list(map(self.type2test, lst))
266 self.assertEqual(self.type2test(b"").join(lst), b"abc")
267 self.assertEqual(self.type2test(b"").join(tuple(lst)), b"abc")
268 self.assertEqual(self.type2test(b"").join(iter(lst)), b"abc")
269 self.assertEqual(self.type2test(b".").join([b"ab", b"cd"]), b"ab.cd")
270 # XXX more...
271
272 def test_index(self):
273 b = self.type2test(b'parrot')
274 self.assertEqual(b.index('p'), 0)
275 self.assertEqual(b.index('rr'), 2)
276 self.assertEqual(b.index('t'), 5)
277 self.assertRaises(ValueError, lambda: b.index('w'))
278
279 def test_count(self):
280 b = self.type2test(b'mississippi')
281 self.assertEqual(b.count(b'i'), 4)
282 self.assertEqual(b.count(b'ss'), 2)
283 self.assertEqual(b.count(b'w'), 0)
284
285 def test_startswith(self):
286 b = self.type2test(b'hello')
287 self.assertFalse(self.type2test().startswith(b"anything"))
288 self.assertTrue(b.startswith(b"hello"))
289 self.assertTrue(b.startswith(b"hel"))
290 self.assertTrue(b.startswith(b"h"))
291 self.assertFalse(b.startswith(b"hellow"))
292 self.assertFalse(b.startswith(b"ha"))
Ezio Melottiba42fd52011-04-26 06:09:45 +0300293 try:
294 b.startswith([b'h'])
295 except TypeError as err:
296 exc = str(err)
297 else:
298 self.fail('startswith unexpectedly succeeded')
299 self.assertIn('bytes', exc)
300 self.assertIn('tuple', exc)
Christian Heimes510711d2008-01-30 11:57:58 +0000301
302 def test_endswith(self):
303 b = self.type2test(b'hello')
304 self.assertFalse(bytearray().endswith(b"anything"))
305 self.assertTrue(b.endswith(b"hello"))
306 self.assertTrue(b.endswith(b"llo"))
307 self.assertTrue(b.endswith(b"o"))
308 self.assertFalse(b.endswith(b"whello"))
309 self.assertFalse(b.endswith(b"no"))
Ezio Melottiba42fd52011-04-26 06:09:45 +0300310 try:
311 b.endswith([b'o'])
312 except TypeError as err:
313 exc = str(err)
314 else:
315 self.fail('endswith unexpectedly succeeded')
316 self.assertIn('bytes', exc)
317 self.assertIn('tuple', exc)
Christian Heimes510711d2008-01-30 11:57:58 +0000318
319 def test_find(self):
320 b = self.type2test(b'mississippi')
321 self.assertEqual(b.find(b'ss'), 2)
322 self.assertEqual(b.find(b'ss', 3), 5)
323 self.assertEqual(b.find(b'ss', 1, 7), 2)
324 self.assertEqual(b.find(b'ss', 1, 3), -1)
325 self.assertEqual(b.find(b'w'), -1)
326 self.assertEqual(b.find(b'mississippian'), -1)
327
328 def test_rfind(self):
329 b = self.type2test(b'mississippi')
330 self.assertEqual(b.rfind(b'ss'), 5)
331 self.assertEqual(b.rfind(b'ss', 3), 5)
332 self.assertEqual(b.rfind(b'ss', 0, 6), 2)
333 self.assertEqual(b.rfind(b'w'), -1)
334 self.assertEqual(b.rfind(b'mississippian'), -1)
335
336 def test_index(self):
337 b = self.type2test(b'world')
338 self.assertEqual(b.index(b'w'), 0)
339 self.assertEqual(b.index(b'orl'), 1)
340 self.assertRaises(ValueError, b.index, b'worm')
341 self.assertRaises(ValueError, b.index, b'ldo')
342
343 def test_rindex(self):
344 # XXX could be more rigorous
345 b = self.type2test(b'world')
346 self.assertEqual(b.rindex(b'w'), 0)
347 self.assertEqual(b.rindex(b'orl'), 1)
348 self.assertRaises(ValueError, b.rindex, b'worm')
349 self.assertRaises(ValueError, b.rindex, b'ldo')
350
351 def test_replace(self):
352 b = self.type2test(b'mississippi')
353 self.assertEqual(b.replace(b'i', b'a'), b'massassappa')
354 self.assertEqual(b.replace(b'ss', b'x'), b'mixixippi')
355
356 def test_split(self):
357 b = self.type2test(b'mississippi')
358 self.assertEqual(b.split(b'i'), [b'm', b'ss', b'ss', b'pp', b''])
359 self.assertEqual(b.split(b'ss'), [b'mi', b'i', b'ippi'])
360 self.assertEqual(b.split(b'w'), [b])
361
362 def test_split_whitespace(self):
363 for b in (b' arf barf ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf',
364 b'arf\fbarf', b'arf\vbarf'):
365 b = self.type2test(b)
366 self.assertEqual(b.split(), [b'arf', b'barf'])
367 self.assertEqual(b.split(None), [b'arf', b'barf'])
368 self.assertEqual(b.split(None, 2), [b'arf', b'barf'])
369 for b in (b'a\x1Cb', b'a\x1Db', b'a\x1Eb', b'a\x1Fb'):
370 b = self.type2test(b)
371 self.assertEqual(b.split(), [b])
372 self.assertEqual(self.type2test(b' a bb c ').split(None, 0), [b'a bb c '])
373 self.assertEqual(self.type2test(b' a bb c ').split(None, 1), [b'a', b'bb c '])
374 self.assertEqual(self.type2test(b' a bb c ').split(None, 2), [b'a', b'bb', b'c '])
375 self.assertEqual(self.type2test(b' a bb c ').split(None, 3), [b'a', b'bb', b'c'])
376
377 def test_split_string_error(self):
378 self.assertRaises(TypeError, self.type2test(b'a b').split, ' ')
379
380 def test_rsplit(self):
381 b = self.type2test(b'mississippi')
382 self.assertEqual(b.rsplit(b'i'), [b'm', b'ss', b'ss', b'pp', b''])
383 self.assertEqual(b.rsplit(b'ss'), [b'mi', b'i', b'ippi'])
384 self.assertEqual(b.rsplit(b'w'), [b])
385
386 def test_rsplit_whitespace(self):
387 for b in (b' arf barf ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf',
388 b'arf\fbarf', b'arf\vbarf'):
389 b = self.type2test(b)
390 self.assertEqual(b.rsplit(), [b'arf', b'barf'])
391 self.assertEqual(b.rsplit(None), [b'arf', b'barf'])
392 self.assertEqual(b.rsplit(None, 2), [b'arf', b'barf'])
393 self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 0), [b' a bb c'])
394 self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 1), [b' a bb', b'c'])
395 self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 2), [b' a', b'bb', b'c'])
396 self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 3), [b'a', b'bb', b'c'])
397
398 def test_rsplit_string_error(self):
399 self.assertRaises(TypeError, self.type2test(b'a b').rsplit, ' ')
400
401 def test_rsplit_unicodewhitespace(self):
402 b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F")
403 self.assertEqual(b.split(), [b'\x1c\x1d\x1e\x1f'])
404 self.assertEqual(b.rsplit(), [b'\x1c\x1d\x1e\x1f'])
405
406 def test_partition(self):
407 b = self.type2test(b'mississippi')
408 self.assertEqual(b.partition(b'ss'), (b'mi', b'ss', b'issippi'))
409 self.assertEqual(b.rpartition(b'w'), (b'', b'', b'mississippi'))
410
411 def test_rpartition(self):
412 b = self.type2test(b'mississippi')
413 self.assertEqual(b.rpartition(b'ss'), (b'missi', b'ss', b'ippi'))
414 self.assertEqual(b.rpartition(b'i'), (b'mississipp', b'i', b''))
415
416 def test_pickling(self):
Hirokazu Yamamotod0d0b652008-10-23 00:38:15 +0000417 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
Christian Heimes510711d2008-01-30 11:57:58 +0000418 for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0":
419 b = self.type2test(b)
420 ps = pickle.dumps(b, proto)
421 q = pickle.loads(ps)
422 self.assertEqual(b, q)
423
424 def test_strip(self):
425 b = self.type2test(b'mississippi')
426 self.assertEqual(b.strip(b'i'), b'mississipp')
427 self.assertEqual(b.strip(b'm'), b'ississippi')
428 self.assertEqual(b.strip(b'pi'), b'mississ')
429 self.assertEqual(b.strip(b'im'), b'ssissipp')
430 self.assertEqual(b.strip(b'pim'), b'ssiss')
431 self.assertEqual(b.strip(b), b'')
432
433 def test_lstrip(self):
434 b = self.type2test(b'mississippi')
435 self.assertEqual(b.lstrip(b'i'), b'mississippi')
436 self.assertEqual(b.lstrip(b'm'), b'ississippi')
437 self.assertEqual(b.lstrip(b'pi'), b'mississippi')
438 self.assertEqual(b.lstrip(b'im'), b'ssissippi')
439 self.assertEqual(b.lstrip(b'pim'), b'ssissippi')
440
441 def test_rstrip(self):
442 b = self.type2test(b'mississippi')
443 self.assertEqual(b.rstrip(b'i'), b'mississipp')
444 self.assertEqual(b.rstrip(b'm'), b'mississippi')
445 self.assertEqual(b.rstrip(b'pi'), b'mississ')
446 self.assertEqual(b.rstrip(b'im'), b'mississipp')
447 self.assertEqual(b.rstrip(b'pim'), b'mississ')
448
449 def test_strip_whitespace(self):
450 b = self.type2test(b' \t\n\r\f\vabc \t\n\r\f\v')
451 self.assertEqual(b.strip(), b'abc')
452 self.assertEqual(b.lstrip(), b'abc \t\n\r\f\v')
453 self.assertEqual(b.rstrip(), b' \t\n\r\f\vabc')
454
455 def test_strip_bytearray(self):
456 self.assertEqual(self.type2test(b'abc').strip(memoryview(b'ac')), b'b')
457 self.assertEqual(self.type2test(b'abc').lstrip(memoryview(b'ac')), b'bc')
458 self.assertEqual(self.type2test(b'abc').rstrip(memoryview(b'ac')), b'ab')
459
460 def test_strip_string_error(self):
461 self.assertRaises(TypeError, self.type2test(b'abc').strip, 'b')
462 self.assertRaises(TypeError, self.type2test(b'abc').lstrip, 'b')
463 self.assertRaises(TypeError, self.type2test(b'abc').rstrip, 'b')
464
465 def test_ord(self):
466 b = self.type2test(b'\0A\x7f\x80\xff')
467 self.assertEqual([ord(b[i:i+1]) for i in range(len(b))],
468 [0, 65, 127, 128, 255])
469
Georg Brandlabc38772009-04-12 15:51:51 +0000470 def test_maketrans(self):
471 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 +0000472 self.assertEqual(self.type2test.maketrans(b'abc', b'xyz'), transtable)
Antoine Pitrou96bb15b2009-10-14 17:18:54 +0000473 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'
474 self.assertEqual(self.type2test.maketrans(b'\375\376\377', b'xyz'), transtable)
Georg Brandlabc38772009-04-12 15:51:51 +0000475 self.assertRaises(ValueError, self.type2test.maketrans, b'abc', b'xyzq')
476 self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 'def')
477
Jesus Ceaac451502011-04-20 17:09:23 +0200478 def test_none_arguments(self):
479 # issue 11828
480 b = self.type2test(b'hello')
481 l = self.type2test(b'l')
482 h = self.type2test(b'h')
483 x = self.type2test(b'x')
484 o = self.type2test(b'o')
485
486 self.assertEqual(2, b.find(l, None))
487 self.assertEqual(3, b.find(l, -2, None))
488 self.assertEqual(2, b.find(l, None, -2))
489 self.assertEqual(0, b.find(h, None, None))
490
491 self.assertEqual(3, b.rfind(l, None))
492 self.assertEqual(3, b.rfind(l, -2, None))
493 self.assertEqual(2, b.rfind(l, None, -2))
494 self.assertEqual(0, b.rfind(h, None, None))
495
496 self.assertEqual(2, b.index(l, None))
497 self.assertEqual(3, b.index(l, -2, None))
498 self.assertEqual(2, b.index(l, None, -2))
499 self.assertEqual(0, b.index(h, None, None))
500
501 self.assertEqual(3, b.rindex(l, None))
502 self.assertEqual(3, b.rindex(l, -2, None))
503 self.assertEqual(2, b.rindex(l, None, -2))
504 self.assertEqual(0, b.rindex(h, None, None))
505
506 self.assertEqual(2, b.count(l, None))
507 self.assertEqual(1, b.count(l, -2, None))
508 self.assertEqual(1, b.count(l, None, -2))
509 self.assertEqual(0, b.count(x, None, None))
510
511 self.assertEqual(True, b.endswith(o, None))
512 self.assertEqual(True, b.endswith(o, -2, None))
513 self.assertEqual(True, b.endswith(l, None, -2))
514 self.assertEqual(False, b.endswith(x, None, None))
515
516 self.assertEqual(True, b.startswith(h, None))
517 self.assertEqual(True, b.startswith(l, -2, None))
518 self.assertEqual(True, b.startswith(h, None, -2))
519 self.assertEqual(False, b.startswith(x, None, None))
520
521 def test_find_etc_raise_correct_error_messages(self):
522 # issue 11828
523 b = self.type2test(b'hello')
524 x = self.type2test(b'x')
525 self.assertRaisesRegexp(TypeError, r'\bfind\b', b.find,
526 x, None, None, None)
527 self.assertRaisesRegexp(TypeError, r'\brfind\b', b.rfind,
528 x, None, None, None)
529 self.assertRaisesRegexp(TypeError, r'\bindex\b', b.index,
530 x, None, None, None)
531 self.assertRaisesRegexp(TypeError, r'\brindex\b', b.rindex,
532 x, None, None, None)
533 self.assertRaisesRegexp(TypeError, r'\bcount\b', b.count,
534 x, None, None, None)
535 self.assertRaisesRegexp(TypeError, r'\bstartswith\b', b.startswith,
536 x, None, None, None)
537 self.assertRaisesRegexp(TypeError, r'\bendswith\b', b.endswith,
538 x, None, None, None)
539
Christian Heimes510711d2008-01-30 11:57:58 +0000540
541class BytesTest(BaseBytesTest):
542 type2test = bytes
543
Antoine Pitrou2f89aa62008-08-02 21:02:48 +0000544 def test_buffer_is_readonly(self):
Antoine Pitrou47d305d2008-08-16 23:28:44 +0000545 fd = os.dup(sys.stdin.fileno())
546 with open(fd, "rb", buffering=0) as f:
Antoine Pitrou2f89aa62008-08-02 21:02:48 +0000547 self.assertRaises(TypeError, f.readinto, b"")
548
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000549 def test_custom(self):
550 class A:
551 def __bytes__(self):
552 return b'abc'
553 self.assertEqual(bytes(A()), b'abc')
554 class A: pass
555 self.assertRaises(TypeError, bytes, A())
556 class A:
557 def __bytes__(self):
558 return None
559 self.assertRaises(TypeError, bytes, A())
560
Antoine Pitrou2f89aa62008-08-02 21:02:48 +0000561
Christian Heimes510711d2008-01-30 11:57:58 +0000562class ByteArrayTest(BaseBytesTest):
563 type2test = bytearray
Guido van Rossumebea9be2007-04-09 00:49:13 +0000564
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000565 def test_nohash(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000566 self.assertRaises(TypeError, hash, bytearray())
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000567
Guido van Rossum254348e2007-11-21 19:29:53 +0000568 def test_bytearray_api(self):
Guido van Rossum75d6f1a2007-05-24 17:52:00 +0000569 short_sample = b"Hello world\n"
Guido van Rossum98297ee2007-11-06 21:34:58 +0000570 sample = short_sample + b"\0"*(20 - len(short_sample))
Guido van Rossumd624f182006-04-24 13:47:05 +0000571 tfn = tempfile.mktemp()
572 try:
573 # Prepare
574 with open(tfn, "wb") as f:
575 f.write(short_sample)
576 # Test readinto
577 with open(tfn, "rb") as f:
Guido van Rossum254348e2007-11-21 19:29:53 +0000578 b = bytearray(20)
Guido van Rossumd624f182006-04-24 13:47:05 +0000579 n = f.readinto(b)
580 self.assertEqual(n, len(short_sample))
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000581 self.assertEqual(list(b), list(sample))
Guido van Rossumd624f182006-04-24 13:47:05 +0000582 # Test writing in binary mode
583 with open(tfn, "wb") as f:
584 f.write(b)
585 with open(tfn, "rb") as f:
586 self.assertEqual(f.read(), sample)
Guido van Rossum13633bb2007-04-13 18:42:35 +0000587 # Text mode is ambiguous; don't test
Guido van Rossumd624f182006-04-24 13:47:05 +0000588 finally:
589 try:
590 os.remove(tfn)
591 except os.error:
592 pass
593
Neal Norwitz6968b052007-02-27 19:02:19 +0000594 def test_reverse(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000595 b = bytearray(b'hello')
Neal Norwitz6968b052007-02-27 19:02:19 +0000596 self.assertEqual(b.reverse(), None)
597 self.assertEqual(b, b'olleh')
Guido van Rossum254348e2007-11-21 19:29:53 +0000598 b = bytearray(b'hello1') # test even number of items
Neal Norwitz6968b052007-02-27 19:02:19 +0000599 b.reverse()
600 self.assertEqual(b, b'1olleh')
Guido van Rossum254348e2007-11-21 19:29:53 +0000601 b = bytearray()
Neal Norwitz6968b052007-02-27 19:02:19 +0000602 b.reverse()
603 self.assertFalse(b)
604
Guido van Rossumd624f182006-04-24 13:47:05 +0000605 def test_regexps(self):
606 def by(s):
Guido van Rossum254348e2007-11-21 19:29:53 +0000607 return bytearray(map(ord, s))
Guido van Rossumd624f182006-04-24 13:47:05 +0000608 b = by("Hello, world")
Antoine Pitroufd036452008-08-19 17:56:33 +0000609 self.assertEqual(re.findall(br"\w+", b), [by("Hello"), by("world")])
Guido van Rossumd624f182006-04-24 13:47:05 +0000610
611 def test_setitem(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000612 b = bytearray([1, 2, 3])
Guido van Rossumd624f182006-04-24 13:47:05 +0000613 b[1] = 100
Guido van Rossum254348e2007-11-21 19:29:53 +0000614 self.assertEqual(b, bytearray([1, 100, 3]))
Guido van Rossumd624f182006-04-24 13:47:05 +0000615 b[-1] = 200
Guido van Rossum254348e2007-11-21 19:29:53 +0000616 self.assertEqual(b, bytearray([1, 100, 200]))
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000617 b[0] = Indexable(10)
Guido van Rossum254348e2007-11-21 19:29:53 +0000618 self.assertEqual(b, bytearray([10, 100, 200]))
Guido van Rossumd624f182006-04-24 13:47:05 +0000619 try:
620 b[3] = 0
621 self.fail("Didn't raise IndexError")
622 except IndexError:
623 pass
624 try:
625 b[-10] = 0
626 self.fail("Didn't raise IndexError")
627 except IndexError:
628 pass
629 try:
630 b[0] = 256
631 self.fail("Didn't raise ValueError")
632 except ValueError:
633 pass
634 try:
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000635 b[0] = Indexable(-1)
Guido van Rossumd624f182006-04-24 13:47:05 +0000636 self.fail("Didn't raise ValueError")
637 except ValueError:
638 pass
639 try:
640 b[0] = None
641 self.fail("Didn't raise TypeError")
642 except TypeError:
643 pass
644
645 def test_delitem(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000646 b = bytearray(range(10))
Guido van Rossumd624f182006-04-24 13:47:05 +0000647 del b[0]
Guido van Rossum254348e2007-11-21 19:29:53 +0000648 self.assertEqual(b, bytearray(range(1, 10)))
Guido van Rossumd624f182006-04-24 13:47:05 +0000649 del b[-1]
Guido van Rossum254348e2007-11-21 19:29:53 +0000650 self.assertEqual(b, bytearray(range(1, 9)))
Guido van Rossumd624f182006-04-24 13:47:05 +0000651 del b[4]
Guido van Rossum254348e2007-11-21 19:29:53 +0000652 self.assertEqual(b, bytearray([1, 2, 3, 4, 6, 7, 8]))
Guido van Rossumd624f182006-04-24 13:47:05 +0000653
654 def test_setslice(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000655 b = bytearray(range(10))
Guido van Rossumd624f182006-04-24 13:47:05 +0000656 self.assertEqual(list(b), list(range(10)))
657
Guido van Rossum254348e2007-11-21 19:29:53 +0000658 b[0:5] = bytearray([1, 1, 1, 1, 1])
659 self.assertEqual(b, bytearray([1, 1, 1, 1, 1, 5, 6, 7, 8, 9]))
Guido van Rossumd624f182006-04-24 13:47:05 +0000660
661 del b[0:-5]
Guido van Rossum254348e2007-11-21 19:29:53 +0000662 self.assertEqual(b, bytearray([5, 6, 7, 8, 9]))
Guido van Rossumd624f182006-04-24 13:47:05 +0000663
Guido van Rossum254348e2007-11-21 19:29:53 +0000664 b[0:0] = bytearray([0, 1, 2, 3, 4])
665 self.assertEqual(b, bytearray(range(10)))
Guido van Rossumd624f182006-04-24 13:47:05 +0000666
Guido van Rossum254348e2007-11-21 19:29:53 +0000667 b[-7:-3] = bytearray([100, 101])
668 self.assertEqual(b, bytearray([0, 1, 2, 100, 101, 7, 8, 9]))
Guido van Rossumd624f182006-04-24 13:47:05 +0000669
670 b[3:5] = [3, 4, 5, 6]
Guido van Rossum254348e2007-11-21 19:29:53 +0000671 self.assertEqual(b, bytearray(range(10)))
Guido van Rossumad7d8d12007-04-13 01:39:34 +0000672
Thomas Wouters9a6e62b2006-08-23 23:20:29 +0000673 b[3:0] = [42, 42, 42]
Guido van Rossum254348e2007-11-21 19:29:53 +0000674 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 +0000675
Thomas Wouters376446d2006-12-19 08:30:14 +0000676 def test_extended_set_del_slice(self):
Mark Dickinsona53f2c92010-01-29 17:29:21 +0000677 indices = (0, None, 1, 3, 19, 300, 1<<333, -1, -2, -31, -300)
Thomas Wouters376446d2006-12-19 08:30:14 +0000678 for start in indices:
679 for stop in indices:
680 # Skip invalid step 0
681 for step in indices[1:]:
682 L = list(range(255))
Guido van Rossum254348e2007-11-21 19:29:53 +0000683 b = bytearray(L)
Thomas Wouters376446d2006-12-19 08:30:14 +0000684 # Make sure we have a slice of exactly the right length,
685 # but with different data.
686 data = L[start:stop:step]
687 data.reverse()
688 L[start:stop:step] = data
689 b[start:stop:step] = data
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000690 self.assertEqual(b, bytearray(L))
Guido van Rossumad7d8d12007-04-13 01:39:34 +0000691
Thomas Wouters376446d2006-12-19 08:30:14 +0000692 del L[start:stop:step]
693 del b[start:stop:step]
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000694 self.assertEqual(b, bytearray(L))
Thomas Wouters376446d2006-12-19 08:30:14 +0000695
Guido van Rossumd624f182006-04-24 13:47:05 +0000696 def test_setslice_trap(self):
697 # This test verifies that we correctly handle assigning self
698 # to a slice of self (the old Lambert Meertens trap).
Guido van Rossum254348e2007-11-21 19:29:53 +0000699 b = bytearray(range(256))
Guido van Rossumd624f182006-04-24 13:47:05 +0000700 b[8:] = b
Guido van Rossum254348e2007-11-21 19:29:53 +0000701 self.assertEqual(b, bytearray(list(range(8)) + list(range(256))))
Guido van Rossumd624f182006-04-24 13:47:05 +0000702
Guido van Rossum13e57212006-04-27 22:54:26 +0000703 def test_iconcat(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000704 b = bytearray(b"abc")
Guido van Rossum13e57212006-04-27 22:54:26 +0000705 b1 = b
Guido van Rossum9c627722007-08-27 18:31:48 +0000706 b += b"def"
707 self.assertEqual(b, b"abcdef")
Guido van Rossum13e57212006-04-27 22:54:26 +0000708 self.assertEqual(b, b1)
Georg Brandlab91fde2009-08-13 08:51:18 +0000709 self.assertTrue(b is b1)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000710 b += b"xyz"
Guido van Rossumad7d8d12007-04-13 01:39:34 +0000711 self.assertEqual(b, b"abcdefxyz")
712 try:
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000713 b += ""
Guido van Rossumad7d8d12007-04-13 01:39:34 +0000714 except TypeError:
715 pass
716 else:
717 self.fail("bytes += unicode didn't raise TypeError")
Guido van Rossum13e57212006-04-27 22:54:26 +0000718
719 def test_irepeat(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000720 b = bytearray(b"abc")
Guido van Rossum13e57212006-04-27 22:54:26 +0000721 b1 = b
722 b *= 3
Guido van Rossum9c627722007-08-27 18:31:48 +0000723 self.assertEqual(b, b"abcabcabc")
Guido van Rossum13e57212006-04-27 22:54:26 +0000724 self.assertEqual(b, b1)
Georg Brandlab91fde2009-08-13 08:51:18 +0000725 self.assertTrue(b is b1)
Guido van Rossum13e57212006-04-27 22:54:26 +0000726
727 def test_irepeat_1char(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000728 b = bytearray(b"x")
Guido van Rossum13e57212006-04-27 22:54:26 +0000729 b1 = b
730 b *= 100
Guido van Rossum98297ee2007-11-06 21:34:58 +0000731 self.assertEqual(b, b"x"*100)
Guido van Rossum13e57212006-04-27 22:54:26 +0000732 self.assertEqual(b, b1)
Georg Brandlab91fde2009-08-13 08:51:18 +0000733 self.assertTrue(b is b1)
Guido van Rossum13e57212006-04-27 22:54:26 +0000734
Guido van Rossum20188312006-05-05 15:15:40 +0000735 def test_alloc(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000736 b = bytearray()
Guido van Rossum20188312006-05-05 15:15:40 +0000737 alloc = b.__alloc__()
Georg Brandlab91fde2009-08-13 08:51:18 +0000738 self.assertTrue(alloc >= 0)
Guido van Rossum20188312006-05-05 15:15:40 +0000739 seq = [alloc]
740 for i in range(100):
Guido van Rossum9c627722007-08-27 18:31:48 +0000741 b += b"x"
Guido van Rossum20188312006-05-05 15:15:40 +0000742 alloc = b.__alloc__()
Georg Brandlab91fde2009-08-13 08:51:18 +0000743 self.assertTrue(alloc >= len(b))
Guido van Rossum20188312006-05-05 15:15:40 +0000744 if alloc not in seq:
745 seq.append(alloc)
Guido van Rossum20188312006-05-05 15:15:40 +0000746
Neal Norwitz6968b052007-02-27 19:02:19 +0000747 def test_extend(self):
748 orig = b'hello'
Guido van Rossum254348e2007-11-21 19:29:53 +0000749 a = bytearray(orig)
Neal Norwitz6968b052007-02-27 19:02:19 +0000750 a.extend(a)
751 self.assertEqual(a, orig + orig)
752 self.assertEqual(a[5:], orig)
Alexandre Vassalotti09121e82007-12-04 05:51:13 +0000753 a = bytearray(b'')
754 # Test iterators that don't have a __length_hint__
755 a.extend(map(int, orig * 25))
756 a.extend(int(x) for x in orig * 25)
757 self.assertEqual(a, orig * 50)
758 self.assertEqual(a[-5:], orig)
759 a = bytearray(b'')
760 a.extend(iter(map(int, orig * 50)))
761 self.assertEqual(a, orig * 50)
762 self.assertEqual(a[-5:], orig)
763 a = bytearray(b'')
764 a.extend(list(map(int, orig * 50)))
765 self.assertEqual(a, orig * 50)
766 self.assertEqual(a[-5:], orig)
767 a = bytearray(b'')
768 self.assertRaises(ValueError, a.extend, [0, 1, 2, 256])
769 self.assertRaises(ValueError, a.extend, [0, 1, 2, -1])
770 self.assertEqual(len(a), 0)
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000771 a = bytearray(b'')
772 a.extend([Indexable(ord('a'))])
773 self.assertEqual(a, b'a')
Guido van Rossumad7d8d12007-04-13 01:39:34 +0000774
Neal Norwitz6968b052007-02-27 19:02:19 +0000775 def test_remove(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000776 b = bytearray(b'hello')
Neal Norwitz6968b052007-02-27 19:02:19 +0000777 b.remove(ord('l'))
778 self.assertEqual(b, b'helo')
779 b.remove(ord('l'))
780 self.assertEqual(b, b'heo')
781 self.assertRaises(ValueError, lambda: b.remove(ord('l')))
782 self.assertRaises(ValueError, lambda: b.remove(400))
Gregory P. Smith60d241f2007-10-16 06:31:30 +0000783 self.assertRaises(TypeError, lambda: b.remove('e'))
Neal Norwitz6968b052007-02-27 19:02:19 +0000784 # remove first and last
785 b.remove(ord('o'))
786 b.remove(ord('h'))
787 self.assertEqual(b, b'e')
Gregory P. Smith60d241f2007-10-16 06:31:30 +0000788 self.assertRaises(TypeError, lambda: b.remove(b'e'))
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000789 b.remove(Indexable(ord('e')))
790 self.assertEqual(b, b'')
Neal Norwitz6968b052007-02-27 19:02:19 +0000791
792 def test_pop(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000793 b = bytearray(b'world')
Neal Norwitz6968b052007-02-27 19:02:19 +0000794 self.assertEqual(b.pop(), ord('d'))
795 self.assertEqual(b.pop(0), ord('w'))
796 self.assertEqual(b.pop(-2), ord('r'))
797 self.assertRaises(IndexError, lambda: b.pop(10))
Guido van Rossum254348e2007-11-21 19:29:53 +0000798 self.assertRaises(OverflowError, lambda: bytearray().pop())
Mark Dickinson424d75a2009-09-06 10:20:23 +0000799 # test for issue #6846
800 self.assertEqual(bytearray(b'\xff').pop(), 0xff)
Neal Norwitz6968b052007-02-27 19:02:19 +0000801
802 def test_nosort(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000803 self.assertRaises(AttributeError, lambda: bytearray().sort())
Neal Norwitz6968b052007-02-27 19:02:19 +0000804
Neal Norwitz6968b052007-02-27 19:02:19 +0000805 def test_append(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000806 b = bytearray(b'hell')
Neal Norwitz6968b052007-02-27 19:02:19 +0000807 b.append(ord('o'))
808 self.assertEqual(b, b'hello')
809 self.assertEqual(b.append(100), None)
Guido van Rossum254348e2007-11-21 19:29:53 +0000810 b = bytearray()
Neal Norwitz6968b052007-02-27 19:02:19 +0000811 b.append(ord('A'))
812 self.assertEqual(len(b), 1)
Gregory P. Smith60d241f2007-10-16 06:31:30 +0000813 self.assertRaises(TypeError, lambda: b.append(b'o'))
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000814 b = bytearray()
815 b.append(Indexable(ord('A')))
816 self.assertEqual(b, b'A')
Neal Norwitz6968b052007-02-27 19:02:19 +0000817
818 def test_insert(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000819 b = bytearray(b'msssspp')
Neal Norwitz6968b052007-02-27 19:02:19 +0000820 b.insert(1, ord('i'))
821 b.insert(4, ord('i'))
822 b.insert(-2, ord('i'))
823 b.insert(1000, ord('i'))
824 self.assertEqual(b, b'mississippi')
Gregory P. Smith60d241f2007-10-16 06:31:30 +0000825 self.assertRaises(TypeError, lambda: b.insert(0, b'1'))
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000826 b = bytearray()
827 b.insert(0, Indexable(ord('A')))
828 self.assertEqual(b, b'A')
Neal Norwitz6968b052007-02-27 19:02:19 +0000829
Benjamin Peterson0f3641c2008-11-19 22:05:52 +0000830 def test_copied(self):
831 # Issue 4348. Make sure that operations that don't mutate the array
832 # copy the bytes.
833 b = bytearray(b'abc')
Amaury Forgeot d'Arcb0c29162008-11-22 22:18:04 +0000834 self.assertFalse(b is b.replace(b'abc', b'cde', 0))
Benjamin Peterson0f3641c2008-11-19 22:05:52 +0000835
836 t = bytearray([i for i in range(256)])
837 x = bytearray(b'')
838 self.assertFalse(x is x.translate(t))
839
Guido van Rossum254348e2007-11-21 19:29:53 +0000840 def test_partition_bytearray_doesnt_share_nullstring(self):
841 a, b, c = bytearray(b"x").partition(b"y")
Guido van Rossum98297ee2007-11-06 21:34:58 +0000842 self.assertEqual(b, b"")
843 self.assertEqual(c, b"")
Georg Brandlab91fde2009-08-13 08:51:18 +0000844 self.assertTrue(b is not c)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000845 b += b"!"
846 self.assertEqual(c, b"")
Guido van Rossum254348e2007-11-21 19:29:53 +0000847 a, b, c = bytearray(b"x").partition(b"y")
Guido van Rossum98297ee2007-11-06 21:34:58 +0000848 self.assertEqual(b, b"")
849 self.assertEqual(c, b"")
850 # Same for rpartition
Guido van Rossum254348e2007-11-21 19:29:53 +0000851 b, c, a = bytearray(b"x").rpartition(b"y")
Guido van Rossum98297ee2007-11-06 21:34:58 +0000852 self.assertEqual(b, b"")
853 self.assertEqual(c, b"")
Georg Brandlab91fde2009-08-13 08:51:18 +0000854 self.assertTrue(b is not c)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000855 b += b"!"
856 self.assertEqual(c, b"")
Guido van Rossum254348e2007-11-21 19:29:53 +0000857 c, b, a = bytearray(b"x").rpartition(b"y")
Guido van Rossum98297ee2007-11-06 21:34:58 +0000858 self.assertEqual(b, b"")
859 self.assertEqual(c, b"")
860
Antoine Pitrou5504e892008-12-06 21:27:53 +0000861 def test_resize_forbidden(self):
862 # #4509: can't resize a bytearray when there are buffer exports, even
863 # if it wouldn't reallocate the underlying buffer.
864 # Furthermore, no destructive changes to the buffer may be applied
865 # before raising the error.
866 b = bytearray(range(10))
867 v = memoryview(b)
868 def resize(n):
869 b[1:-1] = range(n + 1, 2*n - 1)
870 resize(10)
871 orig = b[:]
872 self.assertRaises(BufferError, resize, 11)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000873 self.assertEqual(b, orig)
Antoine Pitrou5504e892008-12-06 21:27:53 +0000874 self.assertRaises(BufferError, resize, 9)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000875 self.assertEqual(b, orig)
Antoine Pitrou5504e892008-12-06 21:27:53 +0000876 self.assertRaises(BufferError, resize, 0)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000877 self.assertEqual(b, orig)
Antoine Pitrou5504e892008-12-06 21:27:53 +0000878 # Other operations implying resize
879 self.assertRaises(BufferError, b.pop, 0)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000880 self.assertEqual(b, orig)
Antoine Pitrou5504e892008-12-06 21:27:53 +0000881 self.assertRaises(BufferError, b.remove, b[1])
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000882 self.assertEqual(b, orig)
Antoine Pitrou5504e892008-12-06 21:27:53 +0000883 def delitem():
884 del b[1]
885 self.assertRaises(BufferError, delitem)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000886 self.assertEqual(b, orig)
Antoine Pitrou5504e892008-12-06 21:27:53 +0000887 # deleting a non-contiguous slice
888 def delslice():
889 b[1:-1:2] = b""
890 self.assertRaises(BufferError, delslice)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000891 self.assertEqual(b, orig)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000892
Antoine Pitrou20d6c152010-01-17 12:43:00 +0000893 def test_empty_bytearray(self):
894 # Issue #7561: operations on empty bytearrays could crash in many
895 # situations, due to a fragile implementation of the
896 # PyByteArray_AS_STRING() C macro.
897 self.assertRaises(ValueError, int, bytearray(b''))
898 self.assertRaises((ValueError, OSError), os.mkdir, bytearray(b''))
899
900
Christian Heimes510711d2008-01-30 11:57:58 +0000901class AssortedBytesTest(unittest.TestCase):
902 #
903 # Test various combinations of bytes and bytearray
904 #
905
906 def setUp(self):
907 self.warning_filters = warnings.filters[:]
908
909 def tearDown(self):
910 warnings.filters = self.warning_filters
911
912 def test_repr_str(self):
913 warnings.simplefilter('ignore', BytesWarning)
914 for f in str, repr:
915 self.assertEqual(f(bytearray()), "bytearray(b'')")
916 self.assertEqual(f(bytearray([0])), "bytearray(b'\\x00')")
917 self.assertEqual(f(bytearray([0, 1, 254, 255])),
918 "bytearray(b'\\x00\\x01\\xfe\\xff')")
919 self.assertEqual(f(b"abc"), "b'abc'")
920 self.assertEqual(f(b"'"), '''b"'"''') # '''
921 self.assertEqual(f(b"'\""), r"""b'\'"'""") # '
922
923 def test_compare_bytes_to_bytearray(self):
924 self.assertEqual(b"abc" == bytes(b"abc"), True)
925 self.assertEqual(b"ab" != bytes(b"abc"), True)
926 self.assertEqual(b"ab" <= bytes(b"abc"), True)
927 self.assertEqual(b"ab" < bytes(b"abc"), True)
928 self.assertEqual(b"abc" >= bytes(b"ab"), True)
929 self.assertEqual(b"abc" > bytes(b"ab"), True)
930
931 self.assertEqual(b"abc" != bytes(b"abc"), False)
932 self.assertEqual(b"ab" == bytes(b"abc"), False)
933 self.assertEqual(b"ab" > bytes(b"abc"), False)
934 self.assertEqual(b"ab" >= bytes(b"abc"), False)
935 self.assertEqual(b"abc" < bytes(b"ab"), False)
936 self.assertEqual(b"abc" <= bytes(b"ab"), False)
937
938 self.assertEqual(bytes(b"abc") == b"abc", True)
939 self.assertEqual(bytes(b"ab") != b"abc", True)
940 self.assertEqual(bytes(b"ab") <= b"abc", True)
941 self.assertEqual(bytes(b"ab") < b"abc", True)
942 self.assertEqual(bytes(b"abc") >= b"ab", True)
943 self.assertEqual(bytes(b"abc") > b"ab", True)
944
945 self.assertEqual(bytes(b"abc") != b"abc", False)
946 self.assertEqual(bytes(b"ab") == b"abc", False)
947 self.assertEqual(bytes(b"ab") > b"abc", False)
948 self.assertEqual(bytes(b"ab") >= b"abc", False)
949 self.assertEqual(bytes(b"abc") < b"ab", False)
950 self.assertEqual(bytes(b"abc") <= b"ab", False)
951
952 def test_doc(self):
Georg Brandlab91fde2009-08-13 08:51:18 +0000953 self.assertTrue(bytearray.__doc__ != None)
954 self.assertTrue(bytearray.__doc__.startswith("bytearray("), bytearray.__doc__)
955 self.assertTrue(bytes.__doc__ != None)
956 self.assertTrue(bytes.__doc__.startswith("bytes("), bytes.__doc__)
Christian Heimes510711d2008-01-30 11:57:58 +0000957
958 def test_from_bytearray(self):
959 sample = bytes(b"Hello world\n\x80\x81\xfe\xff")
960 buf = memoryview(sample)
961 b = bytearray(buf)
962 self.assertEqual(b, bytearray(sample))
963
964 def test_to_str(self):
965 warnings.simplefilter('ignore', BytesWarning)
966 self.assertEqual(str(b''), "b''")
967 self.assertEqual(str(b'x'), "b'x'")
968 self.assertEqual(str(b'\x80'), "b'\\x80'")
969 self.assertEqual(str(bytearray(b'')), "bytearray(b'')")
970 self.assertEqual(str(bytearray(b'x')), "bytearray(b'x')")
971 self.assertEqual(str(bytearray(b'\x80')), "bytearray(b'\\x80')")
972
973 def test_literal(self):
974 tests = [
975 (b"Wonderful spam", "Wonderful spam"),
976 (br"Wonderful spam too", "Wonderful spam too"),
977 (b"\xaa\x00\000\200", "\xaa\x00\000\200"),
978 (br"\xaa\x00\000\200", r"\xaa\x00\000\200"),
979 ]
980 for b, s in tests:
981 self.assertEqual(b, bytearray(s, 'latin-1'))
982 for c in range(128, 256):
983 self.assertRaises(SyntaxError, eval,
984 'b"%s"' % chr(c))
985
986 def test_translate(self):
987 b = b'hello'
Georg Brandlccc47b62008-12-28 11:44:14 +0000988 ba = bytearray(b)
Christian Heimes510711d2008-01-30 11:57:58 +0000989 rosetta = bytearray(range(0, 256))
990 rosetta[ord('o')] = ord('e')
991 c = b.translate(rosetta, b'l')
992 self.assertEqual(b, b'hello')
993 self.assertEqual(c, b'hee')
Georg Brandlccc47b62008-12-28 11:44:14 +0000994 c = ba.translate(rosetta, b'l')
995 self.assertEqual(ba, b'hello')
996 self.assertEqual(c, b'hee')
997 c = b.translate(None, b'e')
998 self.assertEqual(c, b'hllo')
999 c = ba.translate(None, b'e')
1000 self.assertEqual(c, b'hllo')
1001 self.assertRaises(TypeError, b.translate, None, None)
1002 self.assertRaises(TypeError, ba.translate, None, None)
Christian Heimes510711d2008-01-30 11:57:58 +00001003
1004 def test_split_bytearray(self):
1005 self.assertEqual(b'a b'.split(memoryview(b' ')), [b'a', b'b'])
1006
1007 def test_rsplit_bytearray(self):
1008 self.assertEqual(b'a b'.rsplit(memoryview(b' ')), [b'a', b'b'])
1009
Georg Brandleb2c9642008-05-30 12:05:02 +00001010 def test_return_self(self):
1011 # bytearray.replace must always return a new bytearray
1012 b = bytearray()
Georg Brandlab91fde2009-08-13 08:51:18 +00001013 self.assertFalse(b.replace(b'', b'') is b)
Georg Brandleb2c9642008-05-30 12:05:02 +00001014
Barry Warsaw9e9dcd62008-10-17 01:50:37 +00001015 def test_compare(self):
1016 if sys.flags.bytes_warning:
1017 warnings.simplefilter('error', BytesWarning)
1018 self.assertRaises(BytesWarning, operator.eq, b'', '')
1019 self.assertRaises(BytesWarning, operator.ne, b'', '')
1020 self.assertRaises(BytesWarning, operator.eq, bytearray(b''), '')
1021 self.assertRaises(BytesWarning, operator.ne, bytearray(b''), '')
1022 else:
Antoine Pitrou344ec442009-11-01 16:04:09 +00001023 # self.skipTest("BytesWarning is needed for this test: use -bb option")
Barry Warsaw9e9dcd62008-10-17 01:50:37 +00001024 pass
1025
Guido van Rossumd624f182006-04-24 13:47:05 +00001026 # Optimizations:
Guido van Rossume06b6b82006-04-23 07:43:54 +00001027 # __iter__? (optimization)
Guido van Rossumd624f182006-04-24 13:47:05 +00001028 # __reversed__? (optimization)
1029
Guido van Rossumad7d8d12007-04-13 01:39:34 +00001030 # XXX More string methods? (Those that don't use character properties)
1031
Neal Norwitz6968b052007-02-27 19:02:19 +00001032 # There are tests in string_tests.py that are more
1033 # comprehensive for things like split, partition, etc.
1034 # Unfortunately they are all bundled with tests that
1035 # are not appropriate for bytes
Guido van Rossume06b6b82006-04-23 07:43:54 +00001036
Guido van Rossum254348e2007-11-21 19:29:53 +00001037 # I've started porting some of those into bytearray_tests.py, we should port
Gregory P. Smith60d241f2007-10-16 06:31:30 +00001038 # the rest that make sense (the code can be cleaned up to use modern
1039 # unittest methods at the same time).
1040
Guido van Rossum254348e2007-11-21 19:29:53 +00001041class BytearrayPEP3137Test(unittest.TestCase,
Gregory P. Smith60d241f2007-10-16 06:31:30 +00001042 test.buffer_tests.MixinBytesBufferCommonTests):
1043 def marshal(self, x):
Guido van Rossum254348e2007-11-21 19:29:53 +00001044 return bytearray(x)
Gregory P. Smith60d241f2007-10-16 06:31:30 +00001045
1046 def test_returns_new_copy(self):
1047 val = self.marshal(b'1234')
1048 # On immutable types these MAY return a reference to themselves
Guido van Rossum254348e2007-11-21 19:29:53 +00001049 # but on mutable types like bytearray they MUST return a new copy.
Gregory P. Smith60d241f2007-10-16 06:31:30 +00001050 for methname in ('zfill', 'rjust', 'ljust', 'center'):
1051 method = getattr(val, methname)
1052 newval = method(3)
1053 self.assertEqual(val, newval)
1054 self.assertTrue(val is not newval,
1055 methname+' returned self on a mutable object')
1056
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001057
Christian Heimes510711d2008-01-30 11:57:58 +00001058class FixedStringTest(test.string_tests.BaseTest):
Georg Brandlc7885542007-03-06 19:16:20 +00001059
Guido van Rossum9c627722007-08-27 18:31:48 +00001060 def fixtype(self, obj):
1061 if isinstance(obj, str):
1062 return obj.encode("utf-8")
1063 return super().fixtype(obj)
1064
Georg Brandlc7885542007-03-06 19:16:20 +00001065 # Currently the bytes containment testing uses a single integer
1066 # value. This may not be the final design, but until then the
1067 # bytes section with in a bytes containment not valid
1068 def test_contains(self):
1069 pass
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001070 def test_expandtabs(self):
1071 pass
1072 def test_upper(self):
1073 pass
1074 def test_lower(self):
1075 pass
Georg Brandlc7885542007-03-06 19:16:20 +00001076
Christian Heimes510711d2008-01-30 11:57:58 +00001077class ByteArrayAsStringTest(FixedStringTest):
1078 type2test = bytearray
1079
1080class BytesAsStringTest(FixedStringTest):
1081 type2test = bytes
1082
Georg Brandlc7885542007-03-06 19:16:20 +00001083
Guido van Rossum254348e2007-11-21 19:29:53 +00001084class ByteArraySubclass(bytearray):
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001085 pass
1086
Guido van Rossum254348e2007-11-21 19:29:53 +00001087class ByteArraySubclassTest(unittest.TestCase):
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001088
1089 def test_basic(self):
Georg Brandlab91fde2009-08-13 08:51:18 +00001090 self.assertTrue(issubclass(ByteArraySubclass, bytearray))
1091 self.assertTrue(isinstance(ByteArraySubclass(), bytearray))
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001092
1093 a, b = b"abcd", b"efgh"
Guido van Rossum254348e2007-11-21 19:29:53 +00001094 _a, _b = ByteArraySubclass(a), ByteArraySubclass(b)
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001095
1096 # test comparison operators with subclass instances
Georg Brandlab91fde2009-08-13 08:51:18 +00001097 self.assertTrue(_a == _a)
1098 self.assertTrue(_a != _b)
1099 self.assertTrue(_a < _b)
1100 self.assertTrue(_a <= _b)
1101 self.assertTrue(_b >= _a)
1102 self.assertTrue(_b > _a)
1103 self.assertTrue(_a is not a)
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001104
1105 # test concat of subclass instances
1106 self.assertEqual(a + b, _a + _b)
1107 self.assertEqual(a + b, a + _b)
1108 self.assertEqual(a + b, _a + b)
1109
1110 # test repeat
Georg Brandlab91fde2009-08-13 08:51:18 +00001111 self.assertTrue(a*5 == _a*5)
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001112
1113 def test_join(self):
1114 # Make sure join returns a NEW object for single item sequences
1115 # involving a subclass.
1116 # Make sure that it is of the appropriate type.
Guido van Rossum254348e2007-11-21 19:29:53 +00001117 s1 = ByteArraySubclass(b"abcd")
1118 s2 = bytearray().join([s1])
Georg Brandlab91fde2009-08-13 08:51:18 +00001119 self.assertTrue(s1 is not s2)
1120 self.assertTrue(type(s2) is bytearray, type(s2))
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001121
1122 # Test reverse, calling join on subclass
1123 s3 = s1.join([b"abcd"])
Georg Brandlab91fde2009-08-13 08:51:18 +00001124 self.assertTrue(type(s3) is bytearray)
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001125
1126 def test_pickle(self):
Guido van Rossum254348e2007-11-21 19:29:53 +00001127 a = ByteArraySubclass(b"abcd")
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001128 a.x = 10
Guido van Rossum254348e2007-11-21 19:29:53 +00001129 a.y = ByteArraySubclass(b"efgh")
Hirokazu Yamamotod0d0b652008-10-23 00:38:15 +00001130 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001131 b = pickle.loads(pickle.dumps(a, proto))
1132 self.assertNotEqual(id(a), id(b))
1133 self.assertEqual(a, b)
1134 self.assertEqual(a.x, b.x)
1135 self.assertEqual(a.y, b.y)
1136 self.assertEqual(type(a), type(b))
1137 self.assertEqual(type(a.y), type(b.y))
1138
1139 def test_copy(self):
Guido van Rossum254348e2007-11-21 19:29:53 +00001140 a = ByteArraySubclass(b"abcd")
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001141 a.x = 10
Guido van Rossum254348e2007-11-21 19:29:53 +00001142 a.y = ByteArraySubclass(b"efgh")
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001143 for copy_method in (copy.copy, copy.deepcopy):
1144 b = copy_method(a)
1145 self.assertNotEqual(id(a), id(b))
1146 self.assertEqual(a, b)
1147 self.assertEqual(a.x, b.x)
1148 self.assertEqual(a.y, b.y)
1149 self.assertEqual(type(a), type(b))
1150 self.assertEqual(type(a.y), type(b.y))
1151
1152 def test_init_override(self):
Guido van Rossum254348e2007-11-21 19:29:53 +00001153 class subclass(bytearray):
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001154 def __init__(self, newarg=1, *args, **kwargs):
Guido van Rossum254348e2007-11-21 19:29:53 +00001155 bytearray.__init__(self, *args, **kwargs)
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001156 x = subclass(4, source=b"abcd")
1157 self.assertEqual(x, b"abcd")
1158 x = subclass(newarg=4, source=b"abcd")
1159 self.assertEqual(x, b"abcd")
1160
1161
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001162def test_main():
Antoine Pitrou96bb15b2009-10-14 17:18:54 +00001163 test.support.run_unittest(
1164 BytesTest, AssortedBytesTest, BytesAsStringTest,
1165 ByteArrayTest, ByteArrayAsStringTest, ByteArraySubclassTest,
1166 BytearrayPEP3137Test)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001167
1168if __name__ == "__main__":
Guido van Rossuma6c04be2007-11-03 00:24:24 +00001169 test_main()