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 | |
| 3 | XXX This is a mess. Common tests should be moved to buffer_tests.py, |
| 4 | which itself ought to be unified with string_tests.py (and the latter |
| 5 | should be modernized). |
| 6 | """ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 8 | import os |
| 9 | import re |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 10 | import sys |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 11 | import copy |
Florent Xicluna | edf5f0d | 2010-09-03 20:00:37 +0000 | [diff] [blame] | 12 | import functools |
Guido van Rossum | 0ad0812 | 2007-04-11 04:37:43 +0000 | [diff] [blame] | 13 | import pickle |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 14 | import tempfile |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 15 | import unittest |
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 |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 18 | import test.buffer_tests |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 19 | |
Florent Xicluna | edf5f0d | 2010-09-03 20:00:37 +0000 | [diff] [blame] | 20 | |
| 21 | if sys.flags.bytes_warning: |
| 22 | def check_bytes_warnings(func): |
| 23 | @functools.wraps(func) |
| 24 | def wrapper(*args, **kw): |
| 25 | with test.support.check_warnings(('', BytesWarning)): |
| 26 | return func(*args, **kw) |
| 27 | return wrapper |
| 28 | else: |
| 29 | # no-op |
| 30 | def check_bytes_warnings(func): |
| 31 | return func |
| 32 | |
| 33 | |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 34 | class Indexable: |
| 35 | def __init__(self, value=0): |
| 36 | self.value = value |
| 37 | def __index__(self): |
| 38 | return self.value |
| 39 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 40 | |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 41 | class BaseBytesTest(unittest.TestCase): |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 42 | |
| 43 | def test_basics(self): |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 44 | b = self.type2test() |
| 45 | self.assertEqual(type(b), self.type2test) |
| 46 | self.assertEqual(b.__class__, self.type2test) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 47 | |
Alexandre Vassalotti | 41f58a7 | 2010-01-12 01:23:09 +0000 | [diff] [blame] | 48 | def test_copy(self): |
| 49 | a = self.type2test(b"abcd") |
| 50 | for copy_method in (copy.copy, copy.deepcopy): |
| 51 | b = copy_method(a) |
| 52 | self.assertEqual(a, b) |
| 53 | self.assertEqual(type(a), type(b)) |
| 54 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 55 | def test_empty_sequence(self): |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 56 | b = self.type2test() |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 57 | self.assertEqual(len(b), 0) |
| 58 | self.assertRaises(IndexError, lambda: b[0]) |
| 59 | self.assertRaises(IndexError, lambda: b[1]) |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 60 | self.assertRaises(IndexError, lambda: b[sys.maxsize]) |
| 61 | self.assertRaises(IndexError, lambda: b[sys.maxsize+1]) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 62 | self.assertRaises(IndexError, lambda: b[10**100]) |
| 63 | self.assertRaises(IndexError, lambda: b[-1]) |
| 64 | self.assertRaises(IndexError, lambda: b[-2]) |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 65 | self.assertRaises(IndexError, lambda: b[-sys.maxsize]) |
| 66 | self.assertRaises(IndexError, lambda: b[-sys.maxsize-1]) |
| 67 | self.assertRaises(IndexError, lambda: b[-sys.maxsize-2]) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 68 | self.assertRaises(IndexError, lambda: b[-10**100]) |
| 69 | |
| 70 | def test_from_list(self): |
| 71 | ints = list(range(256)) |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 72 | b = self.type2test(i for i in ints) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 73 | self.assertEqual(len(b), 256) |
| 74 | self.assertEqual(list(b), ints) |
| 75 | |
| 76 | def test_from_index(self): |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 77 | b = self.type2test([Indexable(), Indexable(1), Indexable(254), |
| 78 | Indexable(255)]) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 79 | self.assertEqual(list(b), [0, 1, 254, 255]) |
Benjamin Peterson | 4c04583 | 2010-04-16 22:35:32 +0000 | [diff] [blame] | 80 | self.assertRaises(ValueError, self.type2test, [Indexable(-1)]) |
| 81 | self.assertRaises(ValueError, self.type2test, [Indexable(256)]) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 82 | |
| 83 | def test_from_ssize(self): |
Benjamin Peterson | 4c04583 | 2010-04-16 22:35:32 +0000 | [diff] [blame] | 84 | self.assertEqual(self.type2test(0), b'') |
| 85 | self.assertEqual(self.type2test(1), b'\x00') |
| 86 | self.assertEqual(self.type2test(5), b'\x00\x00\x00\x00\x00') |
| 87 | self.assertRaises(ValueError, self.type2test, -1) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 88 | |
Benjamin Peterson | 4c04583 | 2010-04-16 22:35:32 +0000 | [diff] [blame] | 89 | self.assertEqual(self.type2test('0', 'ascii'), b'0') |
| 90 | self.assertEqual(self.type2test(b'0'), b'0') |
Benjamin Peterson | 8380dd5 | 2010-04-16 22:51:37 +0000 | [diff] [blame] | 91 | self.assertRaises(OverflowError, self.type2test, sys.maxsize + 1) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 92 | |
| 93 | def test_constructor_type_errors(self): |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 94 | self.assertRaises(TypeError, self.type2test, 0.0) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 95 | class C: |
| 96 | pass |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 97 | self.assertRaises(TypeError, self.type2test, ["0"]) |
| 98 | self.assertRaises(TypeError, self.type2test, [0.0]) |
| 99 | self.assertRaises(TypeError, self.type2test, [None]) |
| 100 | self.assertRaises(TypeError, self.type2test, [C()]) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 101 | |
| 102 | def test_constructor_value_errors(self): |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 103 | self.assertRaises(ValueError, self.type2test, [-1]) |
| 104 | self.assertRaises(ValueError, self.type2test, [-sys.maxsize]) |
| 105 | self.assertRaises(ValueError, self.type2test, [-sys.maxsize-1]) |
| 106 | self.assertRaises(ValueError, self.type2test, [-sys.maxsize-2]) |
| 107 | self.assertRaises(ValueError, self.type2test, [-10**100]) |
| 108 | self.assertRaises(ValueError, self.type2test, [256]) |
| 109 | self.assertRaises(ValueError, self.type2test, [257]) |
| 110 | self.assertRaises(ValueError, self.type2test, [sys.maxsize]) |
| 111 | self.assertRaises(ValueError, self.type2test, [sys.maxsize+1]) |
| 112 | self.assertRaises(ValueError, self.type2test, [10**100]) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 113 | |
| 114 | def test_compare(self): |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 115 | b1 = self.type2test([1, 2, 3]) |
| 116 | b2 = self.type2test([1, 2, 3]) |
| 117 | b3 = self.type2test([1, 3]) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 118 | |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 119 | self.assertEqual(b1, b2) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 120 | self.assertTrue(b2 != b3) |
| 121 | self.assertTrue(b1 <= b2) |
| 122 | self.assertTrue(b1 <= b3) |
| 123 | self.assertTrue(b1 < b3) |
| 124 | self.assertTrue(b1 >= b2) |
| 125 | self.assertTrue(b3 >= b2) |
| 126 | self.assertTrue(b3 > b2) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 127 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 128 | self.assertFalse(b1 != b2) |
| 129 | self.assertFalse(b2 == b3) |
| 130 | self.assertFalse(b1 > b2) |
| 131 | self.assertFalse(b1 > b3) |
| 132 | self.assertFalse(b1 >= b3) |
| 133 | self.assertFalse(b1 < b2) |
| 134 | self.assertFalse(b3 < b2) |
| 135 | self.assertFalse(b3 <= b2) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 136 | |
Florent Xicluna | edf5f0d | 2010-09-03 20:00:37 +0000 | [diff] [blame] | 137 | @check_bytes_warnings |
Guido van Rossum | 343e97f | 2007-04-09 00:43:24 +0000 | [diff] [blame] | 138 | def test_compare_to_str(self): |
Florent Xicluna | edf5f0d | 2010-09-03 20:00:37 +0000 | [diff] [blame] | 139 | # Byte comparisons with unicode should always fail! |
| 140 | # Test this for all expected byte orders and Unicode character |
| 141 | # sizes. |
| 142 | self.assertEqual(self.type2test(b"\0a\0b\0c") == "abc", False) |
| 143 | self.assertEqual(self.type2test(b"\0\0\0a\0\0\0b\0\0\0c") == "abc", |
| 144 | False) |
| 145 | self.assertEqual(self.type2test(b"a\0b\0c\0") == "abc", False) |
| 146 | self.assertEqual(self.type2test(b"a\0\0\0b\0\0\0c\0\0\0") == "abc", |
| 147 | False) |
| 148 | self.assertEqual(self.type2test() == str(), False) |
| 149 | self.assertEqual(self.type2test() != str(), True) |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 150 | |
| 151 | def test_reversed(self): |
| 152 | input = list(map(ord, "Hello")) |
| 153 | b = self.type2test(input) |
| 154 | output = list(reversed(b)) |
| 155 | input.reverse() |
| 156 | self.assertEqual(output, input) |
| 157 | |
| 158 | def test_getslice(self): |
| 159 | def by(s): |
| 160 | return self.type2test(map(ord, s)) |
| 161 | b = by("Hello, world") |
| 162 | |
| 163 | self.assertEqual(b[:5], by("Hello")) |
| 164 | self.assertEqual(b[1:5], by("ello")) |
| 165 | self.assertEqual(b[5:7], by(", ")) |
| 166 | self.assertEqual(b[7:], by("world")) |
| 167 | self.assertEqual(b[7:12], by("world")) |
| 168 | self.assertEqual(b[7:100], by("world")) |
| 169 | |
| 170 | self.assertEqual(b[:-7], by("Hello")) |
| 171 | self.assertEqual(b[-11:-7], by("ello")) |
| 172 | self.assertEqual(b[-7:-5], by(", ")) |
| 173 | self.assertEqual(b[-5:], by("world")) |
| 174 | self.assertEqual(b[-5:12], by("world")) |
| 175 | self.assertEqual(b[-5:100], by("world")) |
| 176 | self.assertEqual(b[-100:5], by("Hello")) |
| 177 | |
| 178 | def test_extended_getslice(self): |
| 179 | # Test extended slicing by comparing with list slicing. |
| 180 | L = list(range(255)) |
| 181 | b = self.type2test(L) |
| 182 | indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100) |
| 183 | for start in indices: |
| 184 | for stop in indices: |
| 185 | # Skip step 0 (invalid) |
| 186 | for step in indices[1:]: |
| 187 | self.assertEqual(b[start:stop:step], self.type2test(L[start:stop:step])) |
| 188 | |
| 189 | def test_encoding(self): |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 190 | sample = "Hello world\n\u1234\u5678\u9abc" |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 191 | for enc in ("utf-8", "utf-16"): |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 192 | b = self.type2test(sample, enc) |
| 193 | self.assertEqual(b, self.type2test(sample.encode(enc))) |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 194 | self.assertRaises(UnicodeEncodeError, self.type2test, sample, "latin-1") |
| 195 | b = self.type2test(sample, "latin-1", "ignore") |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 196 | self.assertEqual(b, self.type2test(sample[:-3], "utf-8")) |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 197 | |
| 198 | def test_decode(self): |
| 199 | sample = "Hello world\n\u1234\u5678\u9abc\def0\def0" |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 200 | for enc in ("utf-8", "utf-16"): |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 201 | b = self.type2test(sample, enc) |
| 202 | self.assertEqual(b.decode(enc), sample) |
| 203 | sample = "Hello world\n\x80\x81\xfe\xff" |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 204 | b = self.type2test(sample, "latin-1") |
| 205 | self.assertRaises(UnicodeDecodeError, b.decode, "utf-8") |
| 206 | self.assertEqual(b.decode("utf-8", "ignore"), "Hello world\n") |
| 207 | self.assertEqual(b.decode(errors="ignore", encoding="utf-8"), |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 208 | "Hello world\n") |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 209 | # Default encoding is utf-8 |
| 210 | self.assertEqual(self.type2test(b'\xe2\x98\x83').decode(), '\u2603') |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 211 | |
| 212 | def test_from_int(self): |
| 213 | b = self.type2test(0) |
| 214 | self.assertEqual(b, self.type2test()) |
| 215 | b = self.type2test(10) |
| 216 | self.assertEqual(b, self.type2test([0]*10)) |
| 217 | b = self.type2test(10000) |
| 218 | self.assertEqual(b, self.type2test([0]*10000)) |
| 219 | |
| 220 | def test_concat(self): |
| 221 | b1 = self.type2test(b"abc") |
| 222 | b2 = self.type2test(b"def") |
| 223 | self.assertEqual(b1 + b2, b"abcdef") |
| 224 | self.assertEqual(b1 + bytes(b"def"), b"abcdef") |
| 225 | self.assertEqual(bytes(b"def") + b1, b"defabc") |
| 226 | self.assertRaises(TypeError, lambda: b1 + "def") |
| 227 | self.assertRaises(TypeError, lambda: "abc" + b2) |
| 228 | |
| 229 | def test_repeat(self): |
| 230 | for b in b"abc", self.type2test(b"abc"): |
| 231 | self.assertEqual(b * 3, b"abcabcabc") |
| 232 | self.assertEqual(b * 0, b"") |
| 233 | self.assertEqual(b * -1, b"") |
| 234 | self.assertRaises(TypeError, lambda: b * 3.14) |
| 235 | self.assertRaises(TypeError, lambda: 3.14 * b) |
| 236 | # XXX Shouldn't bytes and bytearray agree on what to raise? |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 237 | with self.assertRaises((OverflowError, MemoryError)): |
| 238 | c = b * sys.maxsize |
| 239 | with self.assertRaises((OverflowError, MemoryError)): |
| 240 | b *= sys.maxsize |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 241 | |
| 242 | def test_repeat_1char(self): |
| 243 | self.assertEqual(self.type2test(b'x')*100, self.type2test([ord('x')]*100)) |
| 244 | |
| 245 | def test_contains(self): |
| 246 | b = self.type2test(b"abc") |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 247 | self.assertIn(ord('a'), b) |
| 248 | self.assertIn(int(ord('a')), b) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 249 | self.assertNotIn(200, b) |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 250 | self.assertRaises(ValueError, lambda: 300 in b) |
| 251 | self.assertRaises(ValueError, lambda: -1 in b) |
| 252 | self.assertRaises(TypeError, lambda: None in b) |
| 253 | self.assertRaises(TypeError, lambda: float(ord('a')) in b) |
| 254 | self.assertRaises(TypeError, lambda: "a" in b) |
| 255 | for f in bytes, bytearray: |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 256 | self.assertIn(f(b""), b) |
| 257 | self.assertIn(f(b"a"), b) |
| 258 | self.assertIn(f(b"b"), b) |
| 259 | self.assertIn(f(b"c"), b) |
| 260 | self.assertIn(f(b"ab"), b) |
| 261 | self.assertIn(f(b"bc"), b) |
| 262 | self.assertIn(f(b"abc"), b) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 263 | self.assertNotIn(f(b"ac"), b) |
| 264 | self.assertNotIn(f(b"d"), b) |
| 265 | self.assertNotIn(f(b"dab"), b) |
| 266 | self.assertNotIn(f(b"abd"), b) |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 267 | |
| 268 | def test_fromhex(self): |
| 269 | self.assertRaises(TypeError, self.type2test.fromhex) |
| 270 | self.assertRaises(TypeError, self.type2test.fromhex, 1) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 271 | self.assertEqual(self.type2test.fromhex(''), self.type2test()) |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 272 | b = bytearray([0x1a, 0x2b, 0x30]) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 273 | self.assertEqual(self.type2test.fromhex('1a2B30'), b) |
| 274 | self.assertEqual(self.type2test.fromhex(' 1A 2B 30 '), b) |
| 275 | self.assertEqual(self.type2test.fromhex('0000'), b'\0\0') |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 276 | self.assertRaises(TypeError, self.type2test.fromhex, b'1B') |
| 277 | self.assertRaises(ValueError, self.type2test.fromhex, 'a') |
| 278 | self.assertRaises(ValueError, self.type2test.fromhex, 'rt') |
| 279 | self.assertRaises(ValueError, self.type2test.fromhex, '1a b cd') |
| 280 | self.assertRaises(ValueError, self.type2test.fromhex, '\x00') |
| 281 | self.assertRaises(ValueError, self.type2test.fromhex, '12 \x00 34') |
| 282 | |
| 283 | def test_join(self): |
| 284 | self.assertEqual(self.type2test(b"").join([]), b"") |
| 285 | self.assertEqual(self.type2test(b"").join([b""]), b"") |
| 286 | for lst in [[b"abc"], [b"a", b"bc"], [b"ab", b"c"], [b"a", b"b", b"c"]]: |
| 287 | lst = list(map(self.type2test, lst)) |
| 288 | self.assertEqual(self.type2test(b"").join(lst), b"abc") |
| 289 | self.assertEqual(self.type2test(b"").join(tuple(lst)), b"abc") |
| 290 | self.assertEqual(self.type2test(b"").join(iter(lst)), b"abc") |
| 291 | self.assertEqual(self.type2test(b".").join([b"ab", b"cd"]), b"ab.cd") |
| 292 | # XXX more... |
| 293 | |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 294 | def test_count(self): |
| 295 | b = self.type2test(b'mississippi') |
| 296 | self.assertEqual(b.count(b'i'), 4) |
| 297 | self.assertEqual(b.count(b'ss'), 2) |
| 298 | self.assertEqual(b.count(b'w'), 0) |
| 299 | |
| 300 | def test_startswith(self): |
| 301 | b = self.type2test(b'hello') |
| 302 | self.assertFalse(self.type2test().startswith(b"anything")) |
| 303 | self.assertTrue(b.startswith(b"hello")) |
| 304 | self.assertTrue(b.startswith(b"hel")) |
| 305 | self.assertTrue(b.startswith(b"h")) |
| 306 | self.assertFalse(b.startswith(b"hellow")) |
| 307 | self.assertFalse(b.startswith(b"ha")) |
Ezio Melotti | f2b3f78 | 2011-04-26 06:40:59 +0300 | [diff] [blame] | 308 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 309 | b.startswith([b'h']) |
Ezio Melotti | f2b3f78 | 2011-04-26 06:40:59 +0300 | [diff] [blame] | 310 | exc = str(cm.exception) |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 311 | self.assertIn('bytes', exc) |
| 312 | self.assertIn('tuple', exc) |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 313 | |
| 314 | def test_endswith(self): |
| 315 | b = self.type2test(b'hello') |
| 316 | self.assertFalse(bytearray().endswith(b"anything")) |
| 317 | self.assertTrue(b.endswith(b"hello")) |
| 318 | self.assertTrue(b.endswith(b"llo")) |
| 319 | self.assertTrue(b.endswith(b"o")) |
| 320 | self.assertFalse(b.endswith(b"whello")) |
| 321 | self.assertFalse(b.endswith(b"no")) |
Ezio Melotti | f2b3f78 | 2011-04-26 06:40:59 +0300 | [diff] [blame] | 322 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 323 | b.endswith([b'o']) |
Ezio Melotti | f2b3f78 | 2011-04-26 06:40:59 +0300 | [diff] [blame] | 324 | exc = str(cm.exception) |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 325 | self.assertIn('bytes', exc) |
| 326 | self.assertIn('tuple', exc) |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 327 | |
| 328 | def test_find(self): |
| 329 | b = self.type2test(b'mississippi') |
| 330 | self.assertEqual(b.find(b'ss'), 2) |
| 331 | self.assertEqual(b.find(b'ss', 3), 5) |
| 332 | self.assertEqual(b.find(b'ss', 1, 7), 2) |
| 333 | self.assertEqual(b.find(b'ss', 1, 3), -1) |
| 334 | self.assertEqual(b.find(b'w'), -1) |
| 335 | self.assertEqual(b.find(b'mississippian'), -1) |
| 336 | |
| 337 | def test_rfind(self): |
| 338 | b = self.type2test(b'mississippi') |
| 339 | self.assertEqual(b.rfind(b'ss'), 5) |
| 340 | self.assertEqual(b.rfind(b'ss', 3), 5) |
| 341 | self.assertEqual(b.rfind(b'ss', 0, 6), 2) |
| 342 | self.assertEqual(b.rfind(b'w'), -1) |
| 343 | self.assertEqual(b.rfind(b'mississippian'), -1) |
| 344 | |
| 345 | def test_index(self): |
| 346 | b = self.type2test(b'world') |
| 347 | self.assertEqual(b.index(b'w'), 0) |
| 348 | self.assertEqual(b.index(b'orl'), 1) |
| 349 | self.assertRaises(ValueError, b.index, b'worm') |
| 350 | self.assertRaises(ValueError, b.index, b'ldo') |
| 351 | |
| 352 | def test_rindex(self): |
| 353 | # XXX could be more rigorous |
| 354 | b = self.type2test(b'world') |
| 355 | self.assertEqual(b.rindex(b'w'), 0) |
| 356 | self.assertEqual(b.rindex(b'orl'), 1) |
| 357 | self.assertRaises(ValueError, b.rindex, b'worm') |
| 358 | self.assertRaises(ValueError, b.rindex, b'ldo') |
| 359 | |
| 360 | def test_replace(self): |
| 361 | b = self.type2test(b'mississippi') |
| 362 | self.assertEqual(b.replace(b'i', b'a'), b'massassappa') |
| 363 | self.assertEqual(b.replace(b'ss', b'x'), b'mixixippi') |
| 364 | |
| 365 | def test_split(self): |
| 366 | b = self.type2test(b'mississippi') |
| 367 | self.assertEqual(b.split(b'i'), [b'm', b'ss', b'ss', b'pp', b'']) |
| 368 | self.assertEqual(b.split(b'ss'), [b'mi', b'i', b'ippi']) |
| 369 | self.assertEqual(b.split(b'w'), [b]) |
| 370 | |
| 371 | def test_split_whitespace(self): |
| 372 | for b in (b' arf barf ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf', |
| 373 | b'arf\fbarf', b'arf\vbarf'): |
| 374 | b = self.type2test(b) |
| 375 | self.assertEqual(b.split(), [b'arf', b'barf']) |
| 376 | self.assertEqual(b.split(None), [b'arf', b'barf']) |
| 377 | self.assertEqual(b.split(None, 2), [b'arf', b'barf']) |
| 378 | for b in (b'a\x1Cb', b'a\x1Db', b'a\x1Eb', b'a\x1Fb'): |
| 379 | b = self.type2test(b) |
| 380 | self.assertEqual(b.split(), [b]) |
| 381 | self.assertEqual(self.type2test(b' a bb c ').split(None, 0), [b'a bb c ']) |
| 382 | self.assertEqual(self.type2test(b' a bb c ').split(None, 1), [b'a', b'bb c ']) |
| 383 | self.assertEqual(self.type2test(b' a bb c ').split(None, 2), [b'a', b'bb', b'c ']) |
| 384 | self.assertEqual(self.type2test(b' a bb c ').split(None, 3), [b'a', b'bb', b'c']) |
| 385 | |
| 386 | def test_split_string_error(self): |
| 387 | self.assertRaises(TypeError, self.type2test(b'a b').split, ' ') |
| 388 | |
Antoine Pitrou | f845302 | 2010-01-12 22:05:42 +0000 | [diff] [blame] | 389 | def test_split_unicodewhitespace(self): |
| 390 | b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F") |
| 391 | self.assertEqual(b.split(), [b'\x1c\x1d\x1e\x1f']) |
| 392 | |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 393 | def test_rsplit(self): |
| 394 | b = self.type2test(b'mississippi') |
| 395 | self.assertEqual(b.rsplit(b'i'), [b'm', b'ss', b'ss', b'pp', b'']) |
| 396 | self.assertEqual(b.rsplit(b'ss'), [b'mi', b'i', b'ippi']) |
| 397 | self.assertEqual(b.rsplit(b'w'), [b]) |
| 398 | |
| 399 | def test_rsplit_whitespace(self): |
| 400 | for b in (b' arf barf ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf', |
| 401 | b'arf\fbarf', b'arf\vbarf'): |
| 402 | b = self.type2test(b) |
| 403 | self.assertEqual(b.rsplit(), [b'arf', b'barf']) |
| 404 | self.assertEqual(b.rsplit(None), [b'arf', b'barf']) |
| 405 | self.assertEqual(b.rsplit(None, 2), [b'arf', b'barf']) |
| 406 | self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 0), [b' a bb c']) |
| 407 | self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 1), [b' a bb', b'c']) |
| 408 | self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 2), [b' a', b'bb', b'c']) |
| 409 | self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 3), [b'a', b'bb', b'c']) |
| 410 | |
| 411 | def test_rsplit_string_error(self): |
| 412 | self.assertRaises(TypeError, self.type2test(b'a b').rsplit, ' ') |
| 413 | |
| 414 | def test_rsplit_unicodewhitespace(self): |
| 415 | 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] | 416 | self.assertEqual(b.rsplit(), [b'\x1c\x1d\x1e\x1f']) |
| 417 | |
| 418 | def test_partition(self): |
| 419 | b = self.type2test(b'mississippi') |
| 420 | self.assertEqual(b.partition(b'ss'), (b'mi', b'ss', b'issippi')) |
Antoine Pitrou | f845302 | 2010-01-12 22:05:42 +0000 | [diff] [blame] | 421 | self.assertEqual(b.partition(b'w'), (b'mississippi', b'', b'')) |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 422 | |
| 423 | def test_rpartition(self): |
| 424 | b = self.type2test(b'mississippi') |
| 425 | self.assertEqual(b.rpartition(b'ss'), (b'missi', b'ss', b'ippi')) |
| 426 | self.assertEqual(b.rpartition(b'i'), (b'mississipp', b'i', b'')) |
Antoine Pitrou | f845302 | 2010-01-12 22:05:42 +0000 | [diff] [blame] | 427 | self.assertEqual(b.rpartition(b'w'), (b'', b'', b'mississippi')) |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 428 | |
| 429 | def test_pickling(self): |
Hirokazu Yamamoto | d0d0b65 | 2008-10-23 00:38:15 +0000 | [diff] [blame] | 430 | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 431 | for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0": |
| 432 | b = self.type2test(b) |
| 433 | ps = pickle.dumps(b, proto) |
| 434 | q = pickle.loads(ps) |
| 435 | self.assertEqual(b, q) |
| 436 | |
| 437 | def test_strip(self): |
| 438 | b = self.type2test(b'mississippi') |
| 439 | self.assertEqual(b.strip(b'i'), b'mississipp') |
| 440 | self.assertEqual(b.strip(b'm'), b'ississippi') |
| 441 | self.assertEqual(b.strip(b'pi'), b'mississ') |
| 442 | self.assertEqual(b.strip(b'im'), b'ssissipp') |
| 443 | self.assertEqual(b.strip(b'pim'), b'ssiss') |
| 444 | self.assertEqual(b.strip(b), b'') |
| 445 | |
| 446 | def test_lstrip(self): |
| 447 | b = self.type2test(b'mississippi') |
| 448 | self.assertEqual(b.lstrip(b'i'), b'mississippi') |
| 449 | self.assertEqual(b.lstrip(b'm'), b'ississippi') |
| 450 | self.assertEqual(b.lstrip(b'pi'), b'mississippi') |
| 451 | self.assertEqual(b.lstrip(b'im'), b'ssissippi') |
| 452 | self.assertEqual(b.lstrip(b'pim'), b'ssissippi') |
| 453 | |
| 454 | def test_rstrip(self): |
| 455 | b = self.type2test(b'mississippi') |
| 456 | self.assertEqual(b.rstrip(b'i'), b'mississipp') |
| 457 | self.assertEqual(b.rstrip(b'm'), b'mississippi') |
| 458 | self.assertEqual(b.rstrip(b'pi'), b'mississ') |
| 459 | self.assertEqual(b.rstrip(b'im'), b'mississipp') |
| 460 | self.assertEqual(b.rstrip(b'pim'), b'mississ') |
| 461 | |
| 462 | def test_strip_whitespace(self): |
| 463 | b = self.type2test(b' \t\n\r\f\vabc \t\n\r\f\v') |
| 464 | self.assertEqual(b.strip(), b'abc') |
| 465 | self.assertEqual(b.lstrip(), b'abc \t\n\r\f\v') |
| 466 | self.assertEqual(b.rstrip(), b' \t\n\r\f\vabc') |
| 467 | |
| 468 | def test_strip_bytearray(self): |
| 469 | self.assertEqual(self.type2test(b'abc').strip(memoryview(b'ac')), b'b') |
| 470 | self.assertEqual(self.type2test(b'abc').lstrip(memoryview(b'ac')), b'bc') |
| 471 | self.assertEqual(self.type2test(b'abc').rstrip(memoryview(b'ac')), b'ab') |
| 472 | |
| 473 | def test_strip_string_error(self): |
| 474 | self.assertRaises(TypeError, self.type2test(b'abc').strip, 'b') |
| 475 | self.assertRaises(TypeError, self.type2test(b'abc').lstrip, 'b') |
| 476 | self.assertRaises(TypeError, self.type2test(b'abc').rstrip, 'b') |
| 477 | |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame^] | 478 | def test_center(self): |
| 479 | # Fill character can be either bytes or bytearray (issue 12380) |
| 480 | b = self.type2test(b'abc') |
| 481 | for fill_type in (bytes, bytearray): |
| 482 | self.assertEqual(b.center(7, fill_type(b'-')), |
| 483 | self.type2test(b'--abc--')) |
| 484 | |
| 485 | def test_ljust(self): |
| 486 | # Fill character can be either bytes or bytearray (issue 12380) |
| 487 | b = self.type2test(b'abc') |
| 488 | for fill_type in (bytes, bytearray): |
| 489 | self.assertEqual(b.ljust(7, fill_type(b'-')), |
| 490 | self.type2test(b'abc----')) |
| 491 | |
| 492 | def test_rjust(self): |
| 493 | # Fill character can be either bytes or bytearray (issue 12380) |
| 494 | b = self.type2test(b'abc') |
| 495 | for fill_type in (bytes, bytearray): |
| 496 | self.assertEqual(b.rjust(7, fill_type(b'-')), |
| 497 | self.type2test(b'----abc')) |
| 498 | |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 499 | def test_ord(self): |
| 500 | b = self.type2test(b'\0A\x7f\x80\xff') |
| 501 | self.assertEqual([ord(b[i:i+1]) for i in range(len(b))], |
| 502 | [0, 65, 127, 128, 255]) |
| 503 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 504 | def test_maketrans(self): |
| 505 | 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] | 506 | self.assertEqual(self.type2test.maketrans(b'abc', b'xyz'), transtable) |
Antoine Pitrou | 403ce78 | 2009-10-14 17:14:16 +0000 | [diff] [blame] | 507 | 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' |
| 508 | self.assertEqual(self.type2test.maketrans(b'\375\376\377', b'xyz'), transtable) |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 509 | self.assertRaises(ValueError, self.type2test.maketrans, b'abc', b'xyzq') |
| 510 | self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 'def') |
| 511 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 512 | def test_none_arguments(self): |
| 513 | # issue 11828 |
| 514 | b = self.type2test(b'hello') |
| 515 | l = self.type2test(b'l') |
| 516 | h = self.type2test(b'h') |
| 517 | x = self.type2test(b'x') |
| 518 | o = self.type2test(b'o') |
| 519 | |
| 520 | self.assertEqual(2, b.find(l, None)) |
| 521 | self.assertEqual(3, b.find(l, -2, None)) |
| 522 | self.assertEqual(2, b.find(l, None, -2)) |
| 523 | self.assertEqual(0, b.find(h, None, None)) |
| 524 | |
| 525 | self.assertEqual(3, b.rfind(l, None)) |
| 526 | self.assertEqual(3, b.rfind(l, -2, None)) |
| 527 | self.assertEqual(2, b.rfind(l, None, -2)) |
| 528 | self.assertEqual(0, b.rfind(h, None, None)) |
| 529 | |
| 530 | self.assertEqual(2, b.index(l, None)) |
| 531 | self.assertEqual(3, b.index(l, -2, None)) |
| 532 | self.assertEqual(2, b.index(l, None, -2)) |
| 533 | self.assertEqual(0, b.index(h, None, None)) |
| 534 | |
| 535 | self.assertEqual(3, b.rindex(l, None)) |
| 536 | self.assertEqual(3, b.rindex(l, -2, None)) |
| 537 | self.assertEqual(2, b.rindex(l, None, -2)) |
| 538 | self.assertEqual(0, b.rindex(h, None, None)) |
| 539 | |
| 540 | self.assertEqual(2, b.count(l, None)) |
| 541 | self.assertEqual(1, b.count(l, -2, None)) |
| 542 | self.assertEqual(1, b.count(l, None, -2)) |
| 543 | self.assertEqual(0, b.count(x, None, None)) |
| 544 | |
| 545 | self.assertEqual(True, b.endswith(o, None)) |
| 546 | self.assertEqual(True, b.endswith(o, -2, None)) |
| 547 | self.assertEqual(True, b.endswith(l, None, -2)) |
| 548 | self.assertEqual(False, b.endswith(x, None, None)) |
| 549 | |
| 550 | self.assertEqual(True, b.startswith(h, None)) |
| 551 | self.assertEqual(True, b.startswith(l, -2, None)) |
| 552 | self.assertEqual(True, b.startswith(h, None, -2)) |
| 553 | self.assertEqual(False, b.startswith(x, None, None)) |
| 554 | |
| 555 | def test_find_etc_raise_correct_error_messages(self): |
| 556 | # issue 11828 |
| 557 | b = self.type2test(b'hello') |
| 558 | x = self.type2test(b'x') |
Ezio Melotti | af92842 | 2011-04-20 21:56:21 +0300 | [diff] [blame] | 559 | self.assertRaisesRegex(TypeError, r'\bfind\b', b.find, |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 560 | x, None, None, None) |
Ezio Melotti | af92842 | 2011-04-20 21:56:21 +0300 | [diff] [blame] | 561 | self.assertRaisesRegex(TypeError, r'\brfind\b', b.rfind, |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 562 | x, None, None, None) |
Ezio Melotti | af92842 | 2011-04-20 21:56:21 +0300 | [diff] [blame] | 563 | self.assertRaisesRegex(TypeError, r'\bindex\b', b.index, |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 564 | x, None, None, None) |
Ezio Melotti | af92842 | 2011-04-20 21:56:21 +0300 | [diff] [blame] | 565 | self.assertRaisesRegex(TypeError, r'\brindex\b', b.rindex, |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 566 | x, None, None, None) |
Ezio Melotti | af92842 | 2011-04-20 21:56:21 +0300 | [diff] [blame] | 567 | self.assertRaisesRegex(TypeError, r'\bcount\b', b.count, |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 568 | x, None, None, None) |
Ezio Melotti | af92842 | 2011-04-20 21:56:21 +0300 | [diff] [blame] | 569 | self.assertRaisesRegex(TypeError, r'\bstartswith\b', b.startswith, |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 570 | x, None, None, None) |
Ezio Melotti | af92842 | 2011-04-20 21:56:21 +0300 | [diff] [blame] | 571 | self.assertRaisesRegex(TypeError, r'\bendswith\b', b.endswith, |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 572 | x, None, None, None) |
| 573 | |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 574 | |
| 575 | class BytesTest(BaseBytesTest): |
| 576 | type2test = bytes |
| 577 | |
Antoine Pitrou | 2f89aa6 | 2008-08-02 21:02:48 +0000 | [diff] [blame] | 578 | def test_buffer_is_readonly(self): |
Antoine Pitrou | 47d305d | 2008-08-16 23:28:44 +0000 | [diff] [blame] | 579 | fd = os.dup(sys.stdin.fileno()) |
| 580 | with open(fd, "rb", buffering=0) as f: |
Antoine Pitrou | 2f89aa6 | 2008-08-02 21:02:48 +0000 | [diff] [blame] | 581 | self.assertRaises(TypeError, f.readinto, b"") |
| 582 | |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 583 | def test_custom(self): |
| 584 | class A: |
| 585 | def __bytes__(self): |
| 586 | return b'abc' |
| 587 | self.assertEqual(bytes(A()), b'abc') |
| 588 | class A: pass |
| 589 | self.assertRaises(TypeError, bytes, A()) |
| 590 | class A: |
| 591 | def __bytes__(self): |
| 592 | return None |
| 593 | self.assertRaises(TypeError, bytes, A()) |
| 594 | |
Victor Stinner | 29e762c | 2011-01-05 03:33:28 +0000 | [diff] [blame] | 595 | # Test PyBytes_FromFormat() |
| 596 | def test_from_format(self): |
| 597 | test.support.import_module('ctypes') |
| 598 | from ctypes import pythonapi, py_object, c_int, c_char_p |
| 599 | PyBytes_FromFormat = pythonapi.PyBytes_FromFormat |
| 600 | PyBytes_FromFormat.restype = py_object |
| 601 | |
| 602 | self.assertEqual(PyBytes_FromFormat(b'format'), |
| 603 | b'format') |
| 604 | |
| 605 | self.assertEqual(PyBytes_FromFormat(b'%'), b'%') |
| 606 | self.assertEqual(PyBytes_FromFormat(b'%%'), b'%') |
| 607 | self.assertEqual(PyBytes_FromFormat(b'%%s'), b'%s') |
| 608 | self.assertEqual(PyBytes_FromFormat(b'[%%]'), b'[%]') |
| 609 | self.assertEqual(PyBytes_FromFormat(b'%%%c', c_int(ord('_'))), b'%_') |
| 610 | |
| 611 | self.assertEqual(PyBytes_FromFormat(b'c:%c', c_int(255)), |
| 612 | b'c:\xff') |
| 613 | self.assertEqual(PyBytes_FromFormat(b's:%s', c_char_p(b'cstr')), |
| 614 | b's:cstr') |
| 615 | |
Antoine Pitrou | 2f89aa6 | 2008-08-02 21:02:48 +0000 | [diff] [blame] | 616 | |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 617 | class ByteArrayTest(BaseBytesTest): |
| 618 | type2test = bytearray |
Guido van Rossum | ebea9be | 2007-04-09 00:49:13 +0000 | [diff] [blame] | 619 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 620 | def test_nohash(self): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 621 | self.assertRaises(TypeError, hash, bytearray()) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 622 | |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 623 | def test_bytearray_api(self): |
Guido van Rossum | 75d6f1a | 2007-05-24 17:52:00 +0000 | [diff] [blame] | 624 | short_sample = b"Hello world\n" |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 625 | sample = short_sample + b"\0"*(20 - len(short_sample)) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 626 | tfn = tempfile.mktemp() |
| 627 | try: |
| 628 | # Prepare |
| 629 | with open(tfn, "wb") as f: |
| 630 | f.write(short_sample) |
| 631 | # Test readinto |
| 632 | with open(tfn, "rb") as f: |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 633 | b = bytearray(20) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 634 | n = f.readinto(b) |
| 635 | self.assertEqual(n, len(short_sample)) |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 636 | self.assertEqual(list(b), list(sample)) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 637 | # Test writing in binary mode |
| 638 | with open(tfn, "wb") as f: |
| 639 | f.write(b) |
| 640 | with open(tfn, "rb") as f: |
| 641 | self.assertEqual(f.read(), sample) |
Guido van Rossum | 13633bb | 2007-04-13 18:42:35 +0000 | [diff] [blame] | 642 | # Text mode is ambiguous; don't test |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 643 | finally: |
| 644 | try: |
| 645 | os.remove(tfn) |
| 646 | except os.error: |
| 647 | pass |
| 648 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 649 | def test_reverse(self): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 650 | b = bytearray(b'hello') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 651 | self.assertEqual(b.reverse(), None) |
| 652 | self.assertEqual(b, b'olleh') |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 653 | b = bytearray(b'hello1') # test even number of items |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 654 | b.reverse() |
| 655 | self.assertEqual(b, b'1olleh') |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 656 | b = bytearray() |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 657 | b.reverse() |
| 658 | self.assertFalse(b) |
| 659 | |
Eli Bendersky | 4db28d3 | 2011-03-03 18:21:02 +0000 | [diff] [blame] | 660 | def test_clear(self): |
| 661 | b = bytearray(b'python') |
| 662 | b.clear() |
| 663 | self.assertEqual(b, b'') |
| 664 | |
| 665 | b = bytearray(b'') |
| 666 | b.clear() |
| 667 | self.assertEqual(b, b'') |
| 668 | |
| 669 | b = bytearray(b'') |
| 670 | b.append(ord('r')) |
| 671 | b.clear() |
| 672 | b.append(ord('p')) |
| 673 | self.assertEqual(b, b'p') |
| 674 | |
| 675 | def test_copy(self): |
| 676 | b = bytearray(b'abc') |
| 677 | bb = b.copy() |
| 678 | self.assertEqual(bb, b'abc') |
| 679 | |
| 680 | b = bytearray(b'') |
| 681 | bb = b.copy() |
| 682 | self.assertEqual(bb, b'') |
| 683 | |
| 684 | # test that it's indeed a copy and not a reference |
| 685 | b = bytearray(b'abc') |
| 686 | bb = b.copy() |
| 687 | self.assertEqual(b, bb) |
| 688 | self.assertIsNot(b, bb) |
| 689 | bb.append(ord('d')) |
| 690 | self.assertEqual(bb, b'abcd') |
| 691 | self.assertEqual(b, b'abc') |
| 692 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 693 | def test_regexps(self): |
| 694 | def by(s): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 695 | return bytearray(map(ord, s)) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 696 | b = by("Hello, world") |
Antoine Pitrou | fd03645 | 2008-08-19 17:56:33 +0000 | [diff] [blame] | 697 | self.assertEqual(re.findall(br"\w+", b), [by("Hello"), by("world")]) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 698 | |
| 699 | def test_setitem(self): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 700 | b = bytearray([1, 2, 3]) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 701 | b[1] = 100 |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 702 | self.assertEqual(b, bytearray([1, 100, 3])) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 703 | b[-1] = 200 |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 704 | self.assertEqual(b, bytearray([1, 100, 200])) |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 705 | b[0] = Indexable(10) |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 706 | self.assertEqual(b, bytearray([10, 100, 200])) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 707 | try: |
| 708 | b[3] = 0 |
| 709 | self.fail("Didn't raise IndexError") |
| 710 | except IndexError: |
| 711 | pass |
| 712 | try: |
| 713 | b[-10] = 0 |
| 714 | self.fail("Didn't raise IndexError") |
| 715 | except IndexError: |
| 716 | pass |
| 717 | try: |
| 718 | b[0] = 256 |
| 719 | self.fail("Didn't raise ValueError") |
| 720 | except ValueError: |
| 721 | pass |
| 722 | try: |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 723 | b[0] = Indexable(-1) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 724 | self.fail("Didn't raise ValueError") |
| 725 | except ValueError: |
| 726 | pass |
| 727 | try: |
| 728 | b[0] = None |
| 729 | self.fail("Didn't raise TypeError") |
| 730 | except TypeError: |
| 731 | pass |
| 732 | |
| 733 | def test_delitem(self): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 734 | b = bytearray(range(10)) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 735 | del b[0] |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 736 | self.assertEqual(b, bytearray(range(1, 10))) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 737 | del b[-1] |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 738 | self.assertEqual(b, bytearray(range(1, 9))) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 739 | del b[4] |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 740 | self.assertEqual(b, bytearray([1, 2, 3, 4, 6, 7, 8])) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 741 | |
| 742 | def test_setslice(self): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 743 | b = bytearray(range(10)) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 744 | self.assertEqual(list(b), list(range(10))) |
| 745 | |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 746 | b[0:5] = bytearray([1, 1, 1, 1, 1]) |
| 747 | 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] | 748 | |
| 749 | del b[0:-5] |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 750 | self.assertEqual(b, bytearray([5, 6, 7, 8, 9])) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 751 | |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 752 | b[0:0] = bytearray([0, 1, 2, 3, 4]) |
| 753 | self.assertEqual(b, bytearray(range(10))) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 754 | |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 755 | b[-7:-3] = bytearray([100, 101]) |
| 756 | 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] | 757 | |
| 758 | b[3:5] = [3, 4, 5, 6] |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 759 | self.assertEqual(b, bytearray(range(10))) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 760 | |
Thomas Wouters | 9a6e62b | 2006-08-23 23:20:29 +0000 | [diff] [blame] | 761 | b[3:0] = [42, 42, 42] |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 762 | 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] | 763 | |
Thomas Wouters | 376446d | 2006-12-19 08:30:14 +0000 | [diff] [blame] | 764 | def test_extended_set_del_slice(self): |
Mark Dickinson | bc09964 | 2010-01-29 17:27:24 +0000 | [diff] [blame] | 765 | 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] | 766 | for start in indices: |
| 767 | for stop in indices: |
| 768 | # Skip invalid step 0 |
| 769 | for step in indices[1:]: |
| 770 | L = list(range(255)) |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 771 | b = bytearray(L) |
Thomas Wouters | 376446d | 2006-12-19 08:30:14 +0000 | [diff] [blame] | 772 | # Make sure we have a slice of exactly the right length, |
| 773 | # but with different data. |
| 774 | data = L[start:stop:step] |
| 775 | data.reverse() |
| 776 | L[start:stop:step] = data |
| 777 | b[start:stop:step] = data |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 778 | self.assertEqual(b, bytearray(L)) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 779 | |
Thomas Wouters | 376446d | 2006-12-19 08:30:14 +0000 | [diff] [blame] | 780 | del L[start:stop:step] |
| 781 | del b[start:stop:step] |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 782 | self.assertEqual(b, bytearray(L)) |
Thomas Wouters | 376446d | 2006-12-19 08:30:14 +0000 | [diff] [blame] | 783 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 784 | def test_setslice_trap(self): |
| 785 | # This test verifies that we correctly handle assigning self |
| 786 | # to a slice of self (the old Lambert Meertens trap). |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 787 | b = bytearray(range(256)) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 788 | b[8:] = b |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 789 | self.assertEqual(b, bytearray(list(range(8)) + list(range(256)))) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 790 | |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 791 | def test_iconcat(self): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 792 | b = bytearray(b"abc") |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 793 | b1 = b |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 794 | b += b"def" |
| 795 | self.assertEqual(b, b"abcdef") |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 796 | self.assertEqual(b, b1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 797 | self.assertTrue(b is b1) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 798 | b += b"xyz" |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 799 | self.assertEqual(b, b"abcdefxyz") |
| 800 | try: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 801 | b += "" |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 802 | except TypeError: |
| 803 | pass |
| 804 | else: |
| 805 | self.fail("bytes += unicode didn't raise TypeError") |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 806 | |
| 807 | def test_irepeat(self): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 808 | b = bytearray(b"abc") |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 809 | b1 = b |
| 810 | b *= 3 |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 811 | self.assertEqual(b, b"abcabcabc") |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 812 | self.assertEqual(b, b1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 813 | self.assertTrue(b is b1) |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 814 | |
| 815 | def test_irepeat_1char(self): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 816 | b = bytearray(b"x") |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 817 | b1 = b |
| 818 | b *= 100 |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 819 | self.assertEqual(b, b"x"*100) |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 820 | self.assertEqual(b, b1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 821 | self.assertTrue(b is b1) |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 822 | |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 823 | def test_alloc(self): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 824 | b = bytearray() |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 825 | alloc = b.__alloc__() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 826 | self.assertTrue(alloc >= 0) |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 827 | seq = [alloc] |
| 828 | for i in range(100): |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 829 | b += b"x" |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 830 | alloc = b.__alloc__() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 831 | self.assertTrue(alloc >= len(b)) |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 832 | if alloc not in seq: |
| 833 | seq.append(alloc) |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 834 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 835 | def test_extend(self): |
| 836 | orig = b'hello' |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 837 | a = bytearray(orig) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 838 | a.extend(a) |
| 839 | self.assertEqual(a, orig + orig) |
| 840 | self.assertEqual(a[5:], orig) |
Alexandre Vassalotti | 09121e8 | 2007-12-04 05:51:13 +0000 | [diff] [blame] | 841 | a = bytearray(b'') |
| 842 | # Test iterators that don't have a __length_hint__ |
| 843 | a.extend(map(int, orig * 25)) |
| 844 | a.extend(int(x) for x in orig * 25) |
| 845 | self.assertEqual(a, orig * 50) |
| 846 | self.assertEqual(a[-5:], orig) |
| 847 | a = bytearray(b'') |
| 848 | a.extend(iter(map(int, orig * 50))) |
| 849 | self.assertEqual(a, orig * 50) |
| 850 | self.assertEqual(a[-5:], orig) |
| 851 | a = bytearray(b'') |
| 852 | a.extend(list(map(int, orig * 50))) |
| 853 | self.assertEqual(a, orig * 50) |
| 854 | self.assertEqual(a[-5:], orig) |
| 855 | a = bytearray(b'') |
| 856 | self.assertRaises(ValueError, a.extend, [0, 1, 2, 256]) |
| 857 | self.assertRaises(ValueError, a.extend, [0, 1, 2, -1]) |
| 858 | self.assertEqual(len(a), 0) |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 859 | a = bytearray(b'') |
| 860 | a.extend([Indexable(ord('a'))]) |
| 861 | self.assertEqual(a, b'a') |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 862 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 863 | def test_remove(self): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 864 | b = bytearray(b'hello') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 865 | b.remove(ord('l')) |
| 866 | self.assertEqual(b, b'helo') |
| 867 | b.remove(ord('l')) |
| 868 | self.assertEqual(b, b'heo') |
| 869 | self.assertRaises(ValueError, lambda: b.remove(ord('l'))) |
| 870 | self.assertRaises(ValueError, lambda: b.remove(400)) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 871 | self.assertRaises(TypeError, lambda: b.remove('e')) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 872 | # remove first and last |
| 873 | b.remove(ord('o')) |
| 874 | b.remove(ord('h')) |
| 875 | self.assertEqual(b, b'e') |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 876 | self.assertRaises(TypeError, lambda: b.remove(b'e')) |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 877 | b.remove(Indexable(ord('e'))) |
| 878 | self.assertEqual(b, b'') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 879 | |
| 880 | def test_pop(self): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 881 | b = bytearray(b'world') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 882 | self.assertEqual(b.pop(), ord('d')) |
| 883 | self.assertEqual(b.pop(0), ord('w')) |
| 884 | self.assertEqual(b.pop(-2), ord('r')) |
| 885 | self.assertRaises(IndexError, lambda: b.pop(10)) |
Eli Bendersky | 1bc4f19 | 2011-03-04 04:55:25 +0000 | [diff] [blame] | 886 | self.assertRaises(IndexError, lambda: bytearray().pop()) |
Mark Dickinson | 54a3db9 | 2009-09-06 10:19:23 +0000 | [diff] [blame] | 887 | # test for issue #6846 |
| 888 | self.assertEqual(bytearray(b'\xff').pop(), 0xff) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 889 | |
| 890 | def test_nosort(self): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 891 | self.assertRaises(AttributeError, lambda: bytearray().sort()) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 892 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 893 | def test_append(self): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 894 | b = bytearray(b'hell') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 895 | b.append(ord('o')) |
| 896 | self.assertEqual(b, b'hello') |
| 897 | self.assertEqual(b.append(100), None) |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 898 | b = bytearray() |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 899 | b.append(ord('A')) |
| 900 | self.assertEqual(len(b), 1) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 901 | self.assertRaises(TypeError, lambda: b.append(b'o')) |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 902 | b = bytearray() |
| 903 | b.append(Indexable(ord('A'))) |
| 904 | self.assertEqual(b, b'A') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 905 | |
| 906 | def test_insert(self): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 907 | b = bytearray(b'msssspp') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 908 | b.insert(1, ord('i')) |
| 909 | b.insert(4, ord('i')) |
| 910 | b.insert(-2, ord('i')) |
| 911 | b.insert(1000, ord('i')) |
| 912 | self.assertEqual(b, b'mississippi') |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 913 | self.assertRaises(TypeError, lambda: b.insert(0, b'1')) |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 914 | b = bytearray() |
| 915 | b.insert(0, Indexable(ord('A'))) |
| 916 | self.assertEqual(b, b'A') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 917 | |
Benjamin Peterson | 0f3641c | 2008-11-19 22:05:52 +0000 | [diff] [blame] | 918 | def test_copied(self): |
| 919 | # Issue 4348. Make sure that operations that don't mutate the array |
| 920 | # copy the bytes. |
| 921 | b = bytearray(b'abc') |
Amaury Forgeot d'Arc | b0c2916 | 2008-11-22 22:18:04 +0000 | [diff] [blame] | 922 | self.assertFalse(b is b.replace(b'abc', b'cde', 0)) |
Benjamin Peterson | 0f3641c | 2008-11-19 22:05:52 +0000 | [diff] [blame] | 923 | |
| 924 | t = bytearray([i for i in range(256)]) |
| 925 | x = bytearray(b'') |
| 926 | self.assertFalse(x is x.translate(t)) |
| 927 | |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 928 | def test_partition_bytearray_doesnt_share_nullstring(self): |
| 929 | a, b, c = bytearray(b"x").partition(b"y") |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 930 | self.assertEqual(b, b"") |
| 931 | self.assertEqual(c, b"") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 932 | self.assertTrue(b is not c) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 933 | b += b"!" |
| 934 | self.assertEqual(c, b"") |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 935 | a, b, c = bytearray(b"x").partition(b"y") |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 936 | self.assertEqual(b, b"") |
| 937 | self.assertEqual(c, b"") |
| 938 | # Same for rpartition |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 939 | b, c, a = bytearray(b"x").rpartition(b"y") |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 940 | self.assertEqual(b, b"") |
| 941 | self.assertEqual(c, b"") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 942 | self.assertTrue(b is not c) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 943 | b += b"!" |
| 944 | self.assertEqual(c, b"") |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 945 | c, b, a = bytearray(b"x").rpartition(b"y") |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 946 | self.assertEqual(b, b"") |
| 947 | self.assertEqual(c, b"") |
| 948 | |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 949 | def test_resize_forbidden(self): |
| 950 | # #4509: can't resize a bytearray when there are buffer exports, even |
| 951 | # if it wouldn't reallocate the underlying buffer. |
| 952 | # Furthermore, no destructive changes to the buffer may be applied |
| 953 | # before raising the error. |
| 954 | b = bytearray(range(10)) |
| 955 | v = memoryview(b) |
| 956 | def resize(n): |
| 957 | b[1:-1] = range(n + 1, 2*n - 1) |
| 958 | resize(10) |
| 959 | orig = b[:] |
| 960 | self.assertRaises(BufferError, resize, 11) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 961 | self.assertEqual(b, orig) |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 962 | self.assertRaises(BufferError, resize, 9) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 963 | self.assertEqual(b, orig) |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 964 | self.assertRaises(BufferError, resize, 0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 965 | self.assertEqual(b, orig) |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 966 | # Other operations implying resize |
| 967 | self.assertRaises(BufferError, b.pop, 0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 968 | self.assertEqual(b, orig) |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 969 | self.assertRaises(BufferError, b.remove, b[1]) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 970 | self.assertEqual(b, orig) |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 971 | def delitem(): |
| 972 | del b[1] |
| 973 | self.assertRaises(BufferError, delitem) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 974 | self.assertEqual(b, orig) |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 975 | # deleting a non-contiguous slice |
| 976 | def delslice(): |
| 977 | b[1:-1:2] = b"" |
| 978 | self.assertRaises(BufferError, delslice) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 979 | self.assertEqual(b, orig) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 980 | |
Antoine Pitrou | fc8d6f4 | 2010-01-17 12:38:54 +0000 | [diff] [blame] | 981 | |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 982 | class AssortedBytesTest(unittest.TestCase): |
| 983 | # |
| 984 | # Test various combinations of bytes and bytearray |
| 985 | # |
| 986 | |
Florent Xicluna | edf5f0d | 2010-09-03 20:00:37 +0000 | [diff] [blame] | 987 | @check_bytes_warnings |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 988 | def test_repr_str(self): |
Florent Xicluna | edf5f0d | 2010-09-03 20:00:37 +0000 | [diff] [blame] | 989 | for f in str, repr: |
| 990 | self.assertEqual(f(bytearray()), "bytearray(b'')") |
| 991 | self.assertEqual(f(bytearray([0])), "bytearray(b'\\x00')") |
| 992 | self.assertEqual(f(bytearray([0, 1, 254, 255])), |
| 993 | "bytearray(b'\\x00\\x01\\xfe\\xff')") |
| 994 | self.assertEqual(f(b"abc"), "b'abc'") |
| 995 | self.assertEqual(f(b"'"), '''b"'"''') # ''' |
| 996 | self.assertEqual(f(b"'\""), r"""b'\'"'""") # ' |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 997 | |
| 998 | def test_compare_bytes_to_bytearray(self): |
| 999 | self.assertEqual(b"abc" == bytes(b"abc"), True) |
| 1000 | self.assertEqual(b"ab" != bytes(b"abc"), True) |
| 1001 | self.assertEqual(b"ab" <= bytes(b"abc"), True) |
| 1002 | self.assertEqual(b"ab" < bytes(b"abc"), True) |
| 1003 | self.assertEqual(b"abc" >= bytes(b"ab"), True) |
| 1004 | self.assertEqual(b"abc" > bytes(b"ab"), True) |
| 1005 | |
| 1006 | self.assertEqual(b"abc" != bytes(b"abc"), False) |
| 1007 | self.assertEqual(b"ab" == bytes(b"abc"), False) |
| 1008 | self.assertEqual(b"ab" > bytes(b"abc"), False) |
| 1009 | self.assertEqual(b"ab" >= bytes(b"abc"), False) |
| 1010 | self.assertEqual(b"abc" < bytes(b"ab"), False) |
| 1011 | self.assertEqual(b"abc" <= bytes(b"ab"), False) |
| 1012 | |
| 1013 | self.assertEqual(bytes(b"abc") == b"abc", True) |
| 1014 | self.assertEqual(bytes(b"ab") != b"abc", True) |
| 1015 | self.assertEqual(bytes(b"ab") <= b"abc", True) |
| 1016 | self.assertEqual(bytes(b"ab") < b"abc", True) |
| 1017 | self.assertEqual(bytes(b"abc") >= b"ab", True) |
| 1018 | self.assertEqual(bytes(b"abc") > b"ab", True) |
| 1019 | |
| 1020 | self.assertEqual(bytes(b"abc") != b"abc", False) |
| 1021 | self.assertEqual(bytes(b"ab") == b"abc", False) |
| 1022 | self.assertEqual(bytes(b"ab") > b"abc", False) |
| 1023 | self.assertEqual(bytes(b"ab") >= b"abc", False) |
| 1024 | self.assertEqual(bytes(b"abc") < b"ab", False) |
| 1025 | self.assertEqual(bytes(b"abc") <= b"ab", False) |
| 1026 | |
| 1027 | def test_doc(self): |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 1028 | self.assertIsNotNone(bytearray.__doc__) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1029 | self.assertTrue(bytearray.__doc__.startswith("bytearray("), bytearray.__doc__) |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 1030 | self.assertIsNotNone(bytes.__doc__) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1031 | self.assertTrue(bytes.__doc__.startswith("bytes("), bytes.__doc__) |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 1032 | |
| 1033 | def test_from_bytearray(self): |
| 1034 | sample = bytes(b"Hello world\n\x80\x81\xfe\xff") |
| 1035 | buf = memoryview(sample) |
| 1036 | b = bytearray(buf) |
| 1037 | self.assertEqual(b, bytearray(sample)) |
| 1038 | |
Florent Xicluna | edf5f0d | 2010-09-03 20:00:37 +0000 | [diff] [blame] | 1039 | @check_bytes_warnings |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 1040 | def test_to_str(self): |
Florent Xicluna | edf5f0d | 2010-09-03 20:00:37 +0000 | [diff] [blame] | 1041 | self.assertEqual(str(b''), "b''") |
| 1042 | self.assertEqual(str(b'x'), "b'x'") |
| 1043 | self.assertEqual(str(b'\x80'), "b'\\x80'") |
| 1044 | self.assertEqual(str(bytearray(b'')), "bytearray(b'')") |
| 1045 | self.assertEqual(str(bytearray(b'x')), "bytearray(b'x')") |
| 1046 | self.assertEqual(str(bytearray(b'\x80')), "bytearray(b'\\x80')") |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 1047 | |
| 1048 | def test_literal(self): |
| 1049 | tests = [ |
| 1050 | (b"Wonderful spam", "Wonderful spam"), |
| 1051 | (br"Wonderful spam too", "Wonderful spam too"), |
| 1052 | (b"\xaa\x00\000\200", "\xaa\x00\000\200"), |
| 1053 | (br"\xaa\x00\000\200", r"\xaa\x00\000\200"), |
| 1054 | ] |
| 1055 | for b, s in tests: |
| 1056 | self.assertEqual(b, bytearray(s, 'latin-1')) |
| 1057 | for c in range(128, 256): |
| 1058 | self.assertRaises(SyntaxError, eval, |
| 1059 | 'b"%s"' % chr(c)) |
| 1060 | |
| 1061 | def test_translate(self): |
| 1062 | b = b'hello' |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1063 | ba = bytearray(b) |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 1064 | rosetta = bytearray(range(0, 256)) |
| 1065 | rosetta[ord('o')] = ord('e') |
| 1066 | c = b.translate(rosetta, b'l') |
| 1067 | self.assertEqual(b, b'hello') |
| 1068 | self.assertEqual(c, b'hee') |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1069 | c = ba.translate(rosetta, b'l') |
| 1070 | self.assertEqual(ba, b'hello') |
| 1071 | self.assertEqual(c, b'hee') |
| 1072 | c = b.translate(None, b'e') |
| 1073 | self.assertEqual(c, b'hllo') |
| 1074 | c = ba.translate(None, b'e') |
| 1075 | self.assertEqual(c, b'hllo') |
| 1076 | self.assertRaises(TypeError, b.translate, None, None) |
| 1077 | self.assertRaises(TypeError, ba.translate, None, None) |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 1078 | |
| 1079 | def test_split_bytearray(self): |
| 1080 | self.assertEqual(b'a b'.split(memoryview(b' ')), [b'a', b'b']) |
| 1081 | |
| 1082 | def test_rsplit_bytearray(self): |
| 1083 | self.assertEqual(b'a b'.rsplit(memoryview(b' ')), [b'a', b'b']) |
| 1084 | |
Georg Brandl | eb2c964 | 2008-05-30 12:05:02 +0000 | [diff] [blame] | 1085 | def test_return_self(self): |
| 1086 | # bytearray.replace must always return a new bytearray |
| 1087 | b = bytearray() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1088 | self.assertFalse(b.replace(b'', b'') is b) |
Georg Brandl | eb2c964 | 2008-05-30 12:05:02 +0000 | [diff] [blame] | 1089 | |
Barry Warsaw | 9e9dcd6 | 2008-10-17 01:50:37 +0000 | [diff] [blame] | 1090 | def test_compare(self): |
| 1091 | if sys.flags.bytes_warning: |
Florent Xicluna | edf5f0d | 2010-09-03 20:00:37 +0000 | [diff] [blame] | 1092 | def bytes_warning(): |
| 1093 | return test.support.check_warnings(('', BytesWarning)) |
| 1094 | with bytes_warning(): |
| 1095 | b'' == '' |
| 1096 | with bytes_warning(): |
| 1097 | b'' != '' |
| 1098 | with bytes_warning(): |
| 1099 | bytearray(b'') == '' |
| 1100 | with bytes_warning(): |
| 1101 | bytearray(b'') != '' |
Barry Warsaw | 9e9dcd6 | 2008-10-17 01:50:37 +0000 | [diff] [blame] | 1102 | else: |
Florent Xicluna | edf5f0d | 2010-09-03 20:00:37 +0000 | [diff] [blame] | 1103 | self.skipTest("BytesWarning is needed for this test: use -bb option") |
Barry Warsaw | 9e9dcd6 | 2008-10-17 01:50:37 +0000 | [diff] [blame] | 1104 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 1105 | # Optimizations: |
Guido van Rossum | e06b6b8 | 2006-04-23 07:43:54 +0000 | [diff] [blame] | 1106 | # __iter__? (optimization) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 1107 | # __reversed__? (optimization) |
| 1108 | |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1109 | # XXX More string methods? (Those that don't use character properties) |
| 1110 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1111 | # There are tests in string_tests.py that are more |
| 1112 | # comprehensive for things like split, partition, etc. |
| 1113 | # Unfortunately they are all bundled with tests that |
| 1114 | # are not appropriate for bytes |
Guido van Rossum | e06b6b8 | 2006-04-23 07:43:54 +0000 | [diff] [blame] | 1115 | |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 1116 | # 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] | 1117 | # the rest that make sense (the code can be cleaned up to use modern |
| 1118 | # unittest methods at the same time). |
| 1119 | |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 1120 | class BytearrayPEP3137Test(unittest.TestCase, |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 1121 | test.buffer_tests.MixinBytesBufferCommonTests): |
| 1122 | def marshal(self, x): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 1123 | return bytearray(x) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 1124 | |
| 1125 | def test_returns_new_copy(self): |
| 1126 | val = self.marshal(b'1234') |
| 1127 | # On immutable types these MAY return a reference to themselves |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 1128 | # 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] | 1129 | for methname in ('zfill', 'rjust', 'ljust', 'center'): |
| 1130 | method = getattr(val, methname) |
| 1131 | newval = method(3) |
| 1132 | self.assertEqual(val, newval) |
| 1133 | self.assertTrue(val is not newval, |
| 1134 | methname+' returned self on a mutable object') |
Antoine Pitrou | cbaa6250 | 2010-01-13 15:03:39 +0000 | [diff] [blame] | 1135 | for expr in ('val.split()[0]', 'val.rsplit()[0]', |
| 1136 | 'val.partition(b".")[0]', 'val.rpartition(b".")[2]', |
| 1137 | 'val.splitlines()[0]', 'val.replace(b"", b"")'): |
| 1138 | newval = eval(expr) |
| 1139 | self.assertEqual(val, newval) |
| 1140 | self.assertTrue(val is not newval, |
| 1141 | expr+' returned val on a mutable object') |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 1142 | |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 1143 | class FixedStringTest(test.string_tests.BaseTest): |
Georg Brandl | c788554 | 2007-03-06 19:16:20 +0000 | [diff] [blame] | 1144 | |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 1145 | def fixtype(self, obj): |
| 1146 | if isinstance(obj, str): |
| 1147 | return obj.encode("utf-8") |
| 1148 | return super().fixtype(obj) |
| 1149 | |
Georg Brandl | c788554 | 2007-03-06 19:16:20 +0000 | [diff] [blame] | 1150 | # Currently the bytes containment testing uses a single integer |
| 1151 | # value. This may not be the final design, but until then the |
| 1152 | # bytes section with in a bytes containment not valid |
| 1153 | def test_contains(self): |
| 1154 | pass |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1155 | def test_expandtabs(self): |
| 1156 | pass |
| 1157 | def test_upper(self): |
| 1158 | pass |
| 1159 | def test_lower(self): |
| 1160 | pass |
Georg Brandl | c788554 | 2007-03-06 19:16:20 +0000 | [diff] [blame] | 1161 | |
Christian Heimes | 510711d | 2008-01-30 11:57:58 +0000 | [diff] [blame] | 1162 | class ByteArrayAsStringTest(FixedStringTest): |
| 1163 | type2test = bytearray |
| 1164 | |
| 1165 | class BytesAsStringTest(FixedStringTest): |
| 1166 | type2test = bytes |
| 1167 | |
Georg Brandl | c788554 | 2007-03-06 19:16:20 +0000 | [diff] [blame] | 1168 | |
Alexandre Vassalotti | 41f58a7 | 2010-01-12 01:23:09 +0000 | [diff] [blame] | 1169 | class SubclassTest(unittest.TestCase): |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 1170 | |
| 1171 | def test_basic(self): |
Alexandre Vassalotti | 41f58a7 | 2010-01-12 01:23:09 +0000 | [diff] [blame] | 1172 | self.assertTrue(issubclass(self.subclass2test, self.type2test)) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 1173 | self.assertIsInstance(self.subclass2test(), self.type2test) |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 1174 | |
| 1175 | a, b = b"abcd", b"efgh" |
Alexandre Vassalotti | 41f58a7 | 2010-01-12 01:23:09 +0000 | [diff] [blame] | 1176 | _a, _b = self.subclass2test(a), self.subclass2test(b) |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 1177 | |
| 1178 | # test comparison operators with subclass instances |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1179 | self.assertTrue(_a == _a) |
| 1180 | self.assertTrue(_a != _b) |
| 1181 | self.assertTrue(_a < _b) |
| 1182 | self.assertTrue(_a <= _b) |
| 1183 | self.assertTrue(_b >= _a) |
| 1184 | self.assertTrue(_b > _a) |
| 1185 | self.assertTrue(_a is not a) |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 1186 | |
| 1187 | # test concat of subclass instances |
| 1188 | self.assertEqual(a + b, _a + _b) |
| 1189 | self.assertEqual(a + b, a + _b) |
| 1190 | self.assertEqual(a + b, _a + b) |
| 1191 | |
| 1192 | # test repeat |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1193 | self.assertTrue(a*5 == _a*5) |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 1194 | |
| 1195 | def test_join(self): |
| 1196 | # Make sure join returns a NEW object for single item sequences |
| 1197 | # involving a subclass. |
| 1198 | # Make sure that it is of the appropriate type. |
Alexandre Vassalotti | 41f58a7 | 2010-01-12 01:23:09 +0000 | [diff] [blame] | 1199 | s1 = self.subclass2test(b"abcd") |
| 1200 | s2 = self.type2test().join([s1]) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1201 | self.assertTrue(s1 is not s2) |
Alexandre Vassalotti | 41f58a7 | 2010-01-12 01:23:09 +0000 | [diff] [blame] | 1202 | self.assertTrue(type(s2) is self.type2test, type(s2)) |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 1203 | |
| 1204 | # Test reverse, calling join on subclass |
| 1205 | s3 = s1.join([b"abcd"]) |
Alexandre Vassalotti | 41f58a7 | 2010-01-12 01:23:09 +0000 | [diff] [blame] | 1206 | self.assertTrue(type(s3) is self.type2test) |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 1207 | |
| 1208 | def test_pickle(self): |
Alexandre Vassalotti | 41f58a7 | 2010-01-12 01:23:09 +0000 | [diff] [blame] | 1209 | a = self.subclass2test(b"abcd") |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 1210 | a.x = 10 |
Alexandre Vassalotti | 41f58a7 | 2010-01-12 01:23:09 +0000 | [diff] [blame] | 1211 | a.y = self.subclass2test(b"efgh") |
Hirokazu Yamamoto | d0d0b65 | 2008-10-23 00:38:15 +0000 | [diff] [blame] | 1212 | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 1213 | b = pickle.loads(pickle.dumps(a, proto)) |
| 1214 | self.assertNotEqual(id(a), id(b)) |
| 1215 | self.assertEqual(a, b) |
| 1216 | self.assertEqual(a.x, b.x) |
| 1217 | self.assertEqual(a.y, b.y) |
| 1218 | self.assertEqual(type(a), type(b)) |
| 1219 | self.assertEqual(type(a.y), type(b.y)) |
| 1220 | |
| 1221 | def test_copy(self): |
Alexandre Vassalotti | 41f58a7 | 2010-01-12 01:23:09 +0000 | [diff] [blame] | 1222 | a = self.subclass2test(b"abcd") |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 1223 | a.x = 10 |
Alexandre Vassalotti | 41f58a7 | 2010-01-12 01:23:09 +0000 | [diff] [blame] | 1224 | a.y = self.subclass2test(b"efgh") |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 1225 | for copy_method in (copy.copy, copy.deepcopy): |
| 1226 | b = copy_method(a) |
| 1227 | self.assertNotEqual(id(a), id(b)) |
| 1228 | self.assertEqual(a, b) |
| 1229 | self.assertEqual(a.x, b.x) |
| 1230 | self.assertEqual(a.y, b.y) |
| 1231 | self.assertEqual(type(a), type(b)) |
| 1232 | self.assertEqual(type(a.y), type(b.y)) |
| 1233 | |
Alexandre Vassalotti | 41f58a7 | 2010-01-12 01:23:09 +0000 | [diff] [blame] | 1234 | |
| 1235 | class ByteArraySubclass(bytearray): |
| 1236 | pass |
| 1237 | |
| 1238 | class BytesSubclass(bytes): |
| 1239 | pass |
| 1240 | |
| 1241 | class ByteArraySubclassTest(SubclassTest): |
| 1242 | type2test = bytearray |
| 1243 | subclass2test = ByteArraySubclass |
| 1244 | |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 1245 | def test_init_override(self): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 1246 | class subclass(bytearray): |
Alexandre Vassalotti | 41f58a7 | 2010-01-12 01:23:09 +0000 | [diff] [blame] | 1247 | def __init__(me, newarg=1, *args, **kwargs): |
| 1248 | bytearray.__init__(me, *args, **kwargs) |
| 1249 | x = subclass(4, b"abcd") |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 1250 | x = subclass(4, source=b"abcd") |
| 1251 | self.assertEqual(x, b"abcd") |
| 1252 | x = subclass(newarg=4, source=b"abcd") |
| 1253 | self.assertEqual(x, b"abcd") |
| 1254 | |
| 1255 | |
Alexandre Vassalotti | 41f58a7 | 2010-01-12 01:23:09 +0000 | [diff] [blame] | 1256 | class BytesSubclassTest(SubclassTest): |
| 1257 | type2test = bytes |
| 1258 | subclass2test = BytesSubclass |
| 1259 | |
| 1260 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 1261 | def test_main(): |
Antoine Pitrou | 403ce78 | 2009-10-14 17:14:16 +0000 | [diff] [blame] | 1262 | test.support.run_unittest( |
| 1263 | BytesTest, AssortedBytesTest, BytesAsStringTest, |
Alexandre Vassalotti | 41f58a7 | 2010-01-12 01:23:09 +0000 | [diff] [blame] | 1264 | ByteArrayTest, ByteArrayAsStringTest, BytesSubclassTest, |
| 1265 | ByteArraySubclassTest, BytearrayPEP3137Test) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 1266 | |
| 1267 | if __name__ == "__main__": |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 1268 | test_main() |