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