| 1 |
"""Unit tests for the bytes and bytearray types. |
| 2 |
|
| 3 |
XXX This is a mess. Common tests should be unified with string_tests.py (and |
| 4 |
the latter should be modernized). |
| 5 |
""" |
| 6 |
|
| 7 |
import array |
| 8 |
import operator |
| 9 |
import os |
| 10 |
import re |
| 11 |
import sys |
| 12 |
import copy |
| 13 |
import functools |
| 14 |
import pickle |
| 15 |
import tempfile |
| 16 |
import textwrap |
| 17 |
import threading |
| 18 |
import unittest |
| 19 |
|
| 20 |
import test.support |
| 21 |
from test import support |
| 22 |
from test.support import import_helper |
| 23 |
from test.support import threading_helper |
| 24 |
from test.support import warnings_helper |
| 25 |
import test.string_tests |
| 26 |
import test.list_tests |
| 27 |
from test.support import bigaddrspacetest, MAX_Py_ssize_t |
| 28 |
from test.support.script_helper import assert_python_failure |
| 29 |
|
| 30 |
|
| 31 |
if sys.flags.bytes_warning: |
| 32 |
def check_bytes_warnings(func): |
| 33 |
@functools.wraps(func) |
| 34 |
def wrapper(*args, **kw): |
| 35 |
with warnings_helper.check_warnings(('', BytesWarning)): |
| 36 |
return func(*args, **kw) |
| 37 |
return wrapper |
| 38 |
else: |
| 39 |
# no-op |
| 40 |
def check_bytes_warnings(func): |
| 41 |
return func |
| 42 |
|
| 43 |
|
| 44 |
class Indexable: |
| 45 |
def __init__(self, value=0): |
| 46 |
self.value = value |
| 47 |
def __index__(self): |
| 48 |
return self.value |
| 49 |
|
| 50 |
|
| 51 |
class BaseBytesTest: |
| 52 |
|
| 53 |
def assertTypedEqual(self, actual, expected): |
| 54 |
self.assertIs(type(actual), type(expected)) |
| 55 |
self.assertEqual(actual, expected) |
| 56 |
|
| 57 |
def test_basics(self): |
| 58 |
b = self.type2test() |
| 59 |
self.assertEqual(type(b), self.type2test) |
| 60 |
self.assertEqual(b.__class__, self.type2test) |
| 61 |
|
| 62 |
def test_copy(self): |
| 63 |
a = self.type2test(b"abcd") |
| 64 |
for copy_method in (copy.copy, copy.deepcopy): |
| 65 |
b = copy_method(a) |
| 66 |
self.assertEqual(a, b) |
| 67 |
self.assertEqual(type(a), type(b)) |
| 68 |
|
| 69 |
def test_empty_sequence(self): |
| 70 |
b = self.type2test() |
| 71 |
self.assertEqual(len(b), 0) |
| 72 |
self.assertRaises(IndexError, lambda: b[0]) |
| 73 |
self.assertRaises(IndexError, lambda: b[1]) |
| 74 |
self.assertRaises(IndexError, lambda: b[sys.maxsize]) |
| 75 |
self.assertRaises(IndexError, lambda: b[sys.maxsize+1]) |
| 76 |
self.assertRaises(IndexError, lambda: b[10**100]) |
| 77 |
self.assertRaises(IndexError, lambda: b[-1]) |
| 78 |
self.assertRaises(IndexError, lambda: b[-2]) |
| 79 |
self.assertRaises(IndexError, lambda: b[-sys.maxsize]) |
| 80 |
self.assertRaises(IndexError, lambda: b[-sys.maxsize-1]) |
| 81 |
self.assertRaises(IndexError, lambda: b[-sys.maxsize-2]) |
| 82 |
self.assertRaises(IndexError, lambda: b[-10**100]) |
| 83 |
|
| 84 |
def test_from_iterable(self): |
| 85 |
b = self.type2test(range(256)) |
| 86 |
self.assertEqual(len(b), 256) |
| 87 |
self.assertEqual(list(b), list(range(256))) |
| 88 |
|
| 89 |
# Non-sequence iterable. |
| 90 |
b = self.type2test({42}) |
| 91 |
self.assertEqual(b, b"*") |
| 92 |
b = self.type2test({43, 45}) |
| 93 |
self.assertIn(tuple(b), {(43, 45), (45, 43)}) |
| 94 |
|
| 95 |
# Iterator that has a __length_hint__. |
| 96 |
b = self.type2test(iter(range(256))) |
| 97 |
self.assertEqual(len(b), 256) |
| 98 |
self.assertEqual(list(b), list(range(256))) |
| 99 |
|
| 100 |
# Iterator that doesn't have a __length_hint__. |
| 101 |
b = self.type2test(i for i in range(256) if i % 2) |
| 102 |
self.assertEqual(len(b), 128) |
| 103 |
self.assertEqual(list(b), list(range(256))[1::2]) |
| 104 |
|
| 105 |
# Sequence without __iter__. |
| 106 |
class S: |
| 107 |
def __getitem__(self, i): |
| 108 |
return (1, 2, 3)[i] |
| 109 |
b = self.type2test(S()) |
| 110 |
self.assertEqual(b, b"\x01\x02\x03") |
| 111 |
|
| 112 |
def test_from_tuple(self): |
| 113 |
# There is a special case for tuples. |
| 114 |
b = self.type2test(tuple(range(256))) |
| 115 |
self.assertEqual(len(b), 256) |
| 116 |
self.assertEqual(list(b), list(range(256))) |
| 117 |
b = self.type2test((1, 2, 3)) |
| 118 |
self.assertEqual(b, b"\x01\x02\x03") |
| 119 |
|
| 120 |
def test_from_list(self): |
| 121 |
# There is a special case for lists. |
| 122 |
b = self.type2test(list(range(256))) |
| 123 |
self.assertEqual(len(b), 256) |
| 124 |
self.assertEqual(list(b), list(range(256))) |
| 125 |
b = self.type2test([1, 2, 3]) |
| 126 |
self.assertEqual(b, b"\x01\x02\x03") |
| 127 |
|
| 128 |
def test_from_mutating_list(self): |
| 129 |
# Issue #34973: Crash in bytes constructor with mutating list. |
| 130 |
class X: |
| 131 |
def __index__(self): |
| 132 |
a.clear() |
| 133 |
return 42 |
| 134 |
a = [X(), X()] |
| 135 |
self.assertEqual(bytes(a), b'*') |
| 136 |
|
| 137 |
class Y: |
| 138 |
def __index__(self): |
| 139 |
if len(a) < 1000: |
| 140 |
a.append(self) |
| 141 |
return 42 |
| 142 |
a = [Y()] |
| 143 |
self.assertEqual(bytes(a), b'*' * 1000) # should not crash |
| 144 |
|
| 145 |
def test_from_index(self): |
| 146 |
b = self.type2test([Indexable(), Indexable(1), Indexable(254), |
| 147 |
Indexable(255)]) |
| 148 |
self.assertEqual(list(b), [0, 1, 254, 255]) |
| 149 |
self.assertRaises(ValueError, self.type2test, [Indexable(-1)]) |
| 150 |
self.assertRaises(ValueError, self.type2test, [Indexable(256)]) |
| 151 |
|
| 152 |
def test_from_buffer(self): |
| 153 |
a = self.type2test(array.array('B', [1, 2, 3])) |
| 154 |
self.assertEqual(a, b"\x01\x02\x03") |
| 155 |
a = self.type2test(b"\x01\x02\x03") |
| 156 |
self.assertEqual(a, b"\x01\x02\x03") |
| 157 |
|
| 158 |
# Issues #29159 and #34974. |
| 159 |
# Fallback when __index__ raises a TypeError |
| 160 |
class B(bytes): |
| 161 |
def __index__(self): |
| 162 |
raise TypeError |
| 163 |
|
| 164 |
self.assertEqual(self.type2test(B(b"foobar")), b"foobar") |
| 165 |
|
| 166 |
def test_from_ssize(self): |
| 167 |
self.assertEqual(self.type2test(0), b'') |
| 168 |
self.assertEqual(self.type2test(1), b'\x00') |
| 169 |
self.assertEqual(self.type2test(5), b'\x00\x00\x00\x00\x00') |
| 170 |
self.assertRaises(ValueError, self.type2test, -1) |
| 171 |
|
| 172 |
self.assertEqual(self.type2test('0', 'ascii'), b'0') |
| 173 |
self.assertEqual(self.type2test(b'0'), b'0') |
| 174 |
self.assertRaises(OverflowError, self.type2test, sys.maxsize + 1) |
| 175 |
|
| 176 |
def test_constructor_type_errors(self): |
| 177 |
self.assertRaises(TypeError, self.type2test, 0.0) |
| 178 |
class C: |
| 179 |
pass |
| 180 |
self.assertRaises(TypeError, self.type2test, ["0"]) |
| 181 |
self.assertRaises(TypeError, self.type2test, [0.0]) |
| 182 |
self.assertRaises(TypeError, self.type2test, [None]) |
| 183 |
self.assertRaises(TypeError, self.type2test, [C()]) |
| 184 |
self.assertRaises(TypeError, self.type2test, encoding='ascii') |
| 185 |
self.assertRaises(TypeError, self.type2test, errors='ignore') |
| 186 |
self.assertRaises(TypeError, self.type2test, 0, 'ascii') |
| 187 |
self.assertRaises(TypeError, self.type2test, b'', 'ascii') |
| 188 |
self.assertRaises(TypeError, self.type2test, 0, errors='ignore') |
| 189 |
self.assertRaises(TypeError, self.type2test, b'', errors='ignore') |
| 190 |
self.assertRaises(TypeError, self.type2test, '') |
| 191 |
self.assertRaises(TypeError, self.type2test, '', errors='ignore') |
| 192 |
self.assertRaises(TypeError, self.type2test, '', b'ascii') |
| 193 |
self.assertRaises(TypeError, self.type2test, '', 'ascii', b'ignore') |
| 194 |
|
| 195 |
def test_constructor_value_errors(self): |
| 196 |
self.assertRaises(ValueError, self.type2test, [-1]) |
| 197 |
self.assertRaises(ValueError, self.type2test, [-sys.maxsize]) |
| 198 |
self.assertRaises(ValueError, self.type2test, [-sys.maxsize-1]) |
| 199 |
self.assertRaises(ValueError, self.type2test, [-sys.maxsize-2]) |
| 200 |
self.assertRaises(ValueError, self.type2test, [-10**100]) |
| 201 |
self.assertRaises(ValueError, self.type2test, [256]) |
| 202 |
self.assertRaises(ValueError, self.type2test, [257]) |
| 203 |
self.assertRaises(ValueError, self.type2test, [sys.maxsize]) |
| 204 |
self.assertRaises(ValueError, self.type2test, [sys.maxsize+1]) |
| 205 |
self.assertRaises(ValueError, self.type2test, [10**100]) |
| 206 |
|
| 207 |
@bigaddrspacetest |
| 208 |
def test_constructor_overflow(self): |
| 209 |
size = MAX_Py_ssize_t |
| 210 |
self.assertRaises((OverflowError, MemoryError), self.type2test, size) |
| 211 |
try: |
| 212 |
# Should either pass or raise an error (e.g. on debug builds with |
| 213 |
# additional malloc() overhead), but shouldn't crash. |
| 214 |
bytearray(size - 4) |
| 215 |
except (OverflowError, MemoryError): |
| 216 |
pass |
| 217 |
|
| 218 |
def test_constructor_exceptions(self): |
| 219 |
# Issue #34974: bytes and bytearray constructors replace unexpected |
| 220 |
# exceptions. |
| 221 |
class BadInt: |
| 222 |
def __index__(self): |
| 223 |
1/0 |
| 224 |
self.assertRaises(ZeroDivisionError, self.type2test, BadInt()) |
| 225 |
self.assertRaises(ZeroDivisionError, self.type2test, [BadInt()]) |
| 226 |
|
| 227 |
class BadIterable: |
| 228 |
def __iter__(self): |
| 229 |
1/0 |
| 230 |
self.assertRaises(ZeroDivisionError, self.type2test, BadIterable()) |
| 231 |
|
| 232 |
def test_compare(self): |
| 233 |
b1 = self.type2test([1, 2, 3]) |
| 234 |
b2 = self.type2test([1, 2, 3]) |
| 235 |
b3 = self.type2test([1, 3]) |
| 236 |
|
| 237 |
self.assertEqual(b1, b2) |
| 238 |
self.assertTrue(b2 != b3) |
| 239 |
self.assertTrue(b1 <= b2) |
| 240 |
self.assertTrue(b1 <= b3) |
| 241 |
self.assertTrue(b1 < b3) |
| 242 |
self.assertTrue(b1 >= b2) |
| 243 |
self.assertTrue(b3 >= b2) |
| 244 |
self.assertTrue(b3 > b2) |
| 245 |
|
| 246 |
self.assertFalse(b1 != b2) |
| 247 |
self.assertFalse(b2 == b3) |
| 248 |
self.assertFalse(b1 > b2) |
| 249 |
self.assertFalse(b1 > b3) |
| 250 |
self.assertFalse(b1 >= b3) |
| 251 |
self.assertFalse(b1 < b2) |
| 252 |
self.assertFalse(b3 < b2) |
| 253 |
self.assertFalse(b3 <= b2) |
| 254 |
|
| 255 |
@check_bytes_warnings |
| 256 |
def test_compare_to_str(self): |
| 257 |
# Byte comparisons with unicode should always fail! |
| 258 |
# Test this for all expected byte orders and Unicode character |
| 259 |
# sizes. |
| 260 |
self.assertEqual(self.type2test(b"\0a\0b\0c") == "abc", False) |
| 261 |
self.assertEqual(self.type2test(b"\0\0\0a\0\0\0b\0\0\0c") == "abc", |
| 262 |
False) |
| 263 |
self.assertEqual(self.type2test(b"a\0b\0c\0") == "abc", False) |
| 264 |
self.assertEqual(self.type2test(b"a\0\0\0b\0\0\0c\0\0\0") == "abc", |
| 265 |
False) |
| 266 |
self.assertEqual(self.type2test() == str(), False) |
| 267 |
self.assertEqual(self.type2test() != str(), True) |
| 268 |
|
| 269 |
def test_reversed(self): |
| 270 |
input = list(map(ord, "Hello")) |
| 271 |
b = self.type2test(input) |
| 272 |
output = list(reversed(b)) |
| 273 |
input.reverse() |
| 274 |
self.assertEqual(output, input) |
| 275 |
|
| 276 |
def test_getslice(self): |
| 277 |
def by(s): |
| 278 |
return self.type2test(map(ord, s)) |
| 279 |
b = by("Hello, world") |
| 280 |
|
| 281 |
self.assertEqual(b[:5], by("Hello")) |
| 282 |
self.assertEqual(b[1:5], by("ello")) |
| 283 |
self.assertEqual(b[5:7], by(", ")) |
| 284 |
self.assertEqual(b[7:], by("world")) |
| 285 |
self.assertEqual(b[7:12], by("world")) |
| 286 |
self.assertEqual(b[7:100], by("world")) |
| 287 |
|
| 288 |
self.assertEqual(b[:-7], by("Hello")) |
| 289 |
self.assertEqual(b[-11:-7], by("ello")) |
| 290 |
self.assertEqual(b[-7:-5], by(", ")) |
| 291 |
self.assertEqual(b[-5:], by("world")) |
| 292 |
self.assertEqual(b[-5:12], by("world")) |
| 293 |
self.assertEqual(b[-5:100], by("world")) |
| 294 |
self.assertEqual(b[-100:5], by("Hello")) |
| 295 |
|
| 296 |
def test_extended_getslice(self): |
| 297 |
# Test extended slicing by comparing with list slicing. |
| 298 |
L = list(range(255)) |
| 299 |
b = self.type2test(L) |
| 300 |
indices = (0, None, 1, 3, 19, 100, sys.maxsize, -1, -2, -31, -100) |
| 301 |
for start in indices: |
| 302 |
for stop in indices: |
| 303 |
# Skip step 0 (invalid) |
| 304 |
for step in indices[1:]: |
| 305 |
self.assertEqual(b[start:stop:step], self.type2test(L[start:stop:step])) |
| 306 |
|
| 307 |
def test_encoding(self): |
| 308 |
sample = "Hello world\n\u1234\u5678\u9abc" |
| 309 |
for enc in ("utf-8", "utf-16"): |
| 310 |
b = self.type2test(sample, enc) |
| 311 |
self.assertEqual(b, self.type2test(sample.encode(enc))) |
| 312 |
self.assertRaises(UnicodeEncodeError, self.type2test, sample, "latin-1") |
| 313 |
b = self.type2test(sample, "latin-1", "ignore") |
| 314 |
self.assertEqual(b, self.type2test(sample[:-3], "utf-8")) |
| 315 |
|
| 316 |
def test_decode(self): |
| 317 |
sample = "Hello world\n\u1234\u5678\u9abc" |
| 318 |
for enc in ("utf-8", "utf-16"): |
| 319 |
b = self.type2test(sample, enc) |
| 320 |
self.assertEqual(b.decode(enc), sample) |
| 321 |
sample = "Hello world\n\x80\x81\xfe\xff" |
| 322 |
b = self.type2test(sample, "latin-1") |
| 323 |
self.assertRaises(UnicodeDecodeError, b.decode, "utf-8") |
| 324 |
self.assertEqual(b.decode("utf-8", "ignore"), "Hello world\n") |
| 325 |
self.assertEqual(b.decode(errors="ignore", encoding="utf-8"), |
| 326 |
"Hello world\n") |
| 327 |
# Default encoding is utf-8 |
| 328 |
self.assertEqual(self.type2test(b'\xe2\x98\x83').decode(), '\u2603') |
| 329 |
|
| 330 |
def test_check_encoding_errors(self): |
| 331 |
# bpo-37388: bytes(str) and bytes.encode() must check encoding |
| 332 |
# and errors arguments in dev mode |
| 333 |
invalid = 'Boom, Shaka Laka, Boom!' |
| 334 |
encodings = ('ascii', 'utf8', 'latin1') |
| 335 |
code = textwrap.dedent(f''' |
| 336 |
import sys |
| 337 |
type2test = {self.type2test.__name__} |
| 338 |
encodings = {encodings!r} |
| 339 |
|
| 340 |
for data in ('', 'short string'): |
| 341 |
try: |
| 342 |
type2test(data, encoding={invalid!r}) |
| 343 |
except LookupError: |
| 344 |
pass |
| 345 |
else: |
| 346 |
sys.exit(21) |
| 347 |
|
| 348 |
for encoding in encodings: |
| 349 |
try: |
| 350 |
type2test(data, encoding=encoding, errors={invalid!r}) |
| 351 |
except LookupError: |
| 352 |
pass |
| 353 |
else: |
| 354 |
sys.exit(22) |
| 355 |
|
| 356 |
for data in (b'', b'short string'): |
| 357 |
data = type2test(data) |
| 358 |
print(repr(data)) |
| 359 |
try: |
| 360 |
data.decode(encoding={invalid!r}) |
| 361 |
except LookupError: |
| 362 |
sys.exit(10) |
| 363 |
else: |
| 364 |
sys.exit(23) |
| 365 |
|
| 366 |
try: |
| 367 |
data.decode(errors={invalid!r}) |
| 368 |
except LookupError: |
| 369 |
pass |
| 370 |
else: |
| 371 |
sys.exit(24) |
| 372 |
|
| 373 |
for encoding in encodings: |
| 374 |
try: |
| 375 |
data.decode(encoding=encoding, errors={invalid!r}) |
| 376 |
except LookupError: |
| 377 |
pass |
| 378 |
else: |
| 379 |
sys.exit(25) |
| 380 |
|
| 381 |
sys.exit(10) |
| 382 |
''') |
| 383 |
proc = assert_python_failure('-X', 'dev', '-c', code) |
| 384 |
self.assertEqual(proc.rc, 10, proc) |
| 385 |
|
| 386 |
def test_from_int(self): |
| 387 |
b = self.type2test(0) |
| 388 |
self.assertEqual(b, self.type2test()) |
| 389 |
b = self.type2test(10) |
| 390 |
self.assertEqual(b, self.type2test([0]*10)) |
| 391 |
b = self.type2test(10000) |
| 392 |
self.assertEqual(b, self.type2test([0]*10000)) |
| 393 |
|
| 394 |
def test_concat(self): |
| 395 |
b1 = self.type2test(b"abc") |
| 396 |
b2 = self.type2test(b"def") |
| 397 |
self.assertEqual(b1 + b2, b"abcdef") |
| 398 |
self.assertEqual(b1 + bytes(b"def"), b"abcdef") |
| 399 |
self.assertEqual(bytes(b"def") + b1, b"defabc") |
| 400 |
self.assertRaises(TypeError, lambda: b1 + "def") |
| 401 |
self.assertRaises(TypeError, lambda: "abc" + b2) |
| 402 |
|
| 403 |
def test_repeat(self): |
| 404 |
for b in b"abc", self.type2test(b"abc"): |
| 405 |
self.assertEqual(b * 3, b"abcabcabc") |
| 406 |
self.assertEqual(b * 0, b"") |
| 407 |
self.assertEqual(b * -1, b"") |
| 408 |
self.assertRaises(TypeError, lambda: b * 3.14) |
| 409 |
self.assertRaises(TypeError, lambda: 3.14 * b) |
| 410 |
# XXX Shouldn't bytes and bytearray agree on what to raise? |
| 411 |
with self.assertRaises((OverflowError, MemoryError)): |
| 412 |
c = b * sys.maxsize |
| 413 |
with self.assertRaises((OverflowError, MemoryError)): |
| 414 |
b *= sys.maxsize |
| 415 |
|
| 416 |
def test_repeat_1char(self): |
| 417 |
self.assertEqual(self.type2test(b'x')*100, self.type2test([ord('x')]*100)) |
| 418 |
|
| 419 |
def test_contains(self): |
| 420 |
b = self.type2test(b"abc") |
| 421 |
self.assertIn(ord('a'), b) |
| 422 |
self.assertIn(int(ord('a')), b) |
| 423 |
self.assertNotIn(200, b) |
| 424 |
self.assertRaises(ValueError, lambda: 300 in b) |
| 425 |
self.assertRaises(ValueError, lambda: -1 in b) |
| 426 |
self.assertRaises(ValueError, lambda: sys.maxsize+1 in b) |
| 427 |
self.assertRaises(TypeError, lambda: None in b) |
| 428 |
self.assertRaises(TypeError, lambda: float(ord('a')) in b) |
| 429 |
self.assertRaises(TypeError, lambda: "a" in b) |
| 430 |
for f in bytes, bytearray: |
| 431 |
self.assertIn(f(b""), b) |
| 432 |
self.assertIn(f(b"a"), b) |
| 433 |
self.assertIn(f(b"b"), b) |
| 434 |
self.assertIn(f(b"c"), b) |
| 435 |
self.assertIn(f(b"ab"), b) |
| 436 |
self.assertIn(f(b"bc"), b) |
| 437 |
self.assertIn(f(b"abc"), b) |
| 438 |
self.assertNotIn(f(b"ac"), b) |
| 439 |
self.assertNotIn(f(b"d"), b) |
| 440 |
self.assertNotIn(f(b"dab"), b) |
| 441 |
self.assertNotIn(f(b"abd"), b) |
| 442 |
|
| 443 |
def test_fromhex(self): |
| 444 |
self.assertRaises(TypeError, self.type2test.fromhex) |
| 445 |
self.assertRaises(TypeError, self.type2test.fromhex, 1) |
| 446 |
self.assertEqual(self.type2test.fromhex(''), self.type2test()) |
| 447 |
b = bytearray([0x1a, 0x2b, 0x30]) |
| 448 |
self.assertEqual(self.type2test.fromhex('1a2B30'), b) |
| 449 |
self.assertEqual(self.type2test.fromhex(' 1A 2B 30 '), b) |
| 450 |
|
| 451 |
# check that ASCII whitespace is ignored |
| 452 |
self.assertEqual(self.type2test.fromhex(' 1A\n2B\t30\v'), b) |
| 453 |
self.assertEqual(self.type2test.fromhex(b' 1A\n2B\t30\v'), b) |
| 454 |
for c in "\x09\x0A\x0B\x0C\x0D\x20": |
| 455 |
self.assertEqual(self.type2test.fromhex(c), self.type2test()) |
| 456 |
for c in "\x1C\x1D\x1E\x1F\x85\xa0\u2000\u2002\u2028": |
| 457 |
self.assertRaises(ValueError, self.type2test.fromhex, c) |
| 458 |
|
| 459 |
# Check that we can parse bytes and bytearray |
| 460 |
tests = [ |
| 461 |
("bytes", bytes), |
| 462 |
("bytearray", bytearray), |
| 463 |
("memoryview", memoryview), |
| 464 |
("array.array", lambda bs: array.array('B', bs)), |
| 465 |
] |
| 466 |
for name, factory in tests: |
| 467 |
with self.subTest(name=name): |
| 468 |
self.assertEqual(self.type2test.fromhex(factory(b' 1A 2B 30 ')), b) |
| 469 |
|
| 470 |
# Invalid bytes are rejected |
| 471 |
for u8 in b"\0\x1C\x1D\x1E\x1F\x85\xa0": |
| 472 |
b = bytes([30, 31, u8]) |
| 473 |
self.assertRaises(ValueError, self.type2test.fromhex, b) |
| 474 |
|
| 475 |
self.assertEqual(self.type2test.fromhex('0000'), b'\0\0') |
| 476 |
with self.assertRaisesRegex( |
| 477 |
TypeError, |
| 478 |
r'fromhex\(\) argument must be str or bytes-like, not tuple', |
| 479 |
): |
| 480 |
self.type2test.fromhex(()) |
| 481 |
self.assertRaises(ValueError, self.type2test.fromhex, 'a') |
| 482 |
self.assertRaises(ValueError, self.type2test.fromhex, 'rt') |
| 483 |
self.assertRaises(ValueError, self.type2test.fromhex, '1a b cd') |
| 484 |
self.assertRaises(ValueError, self.type2test.fromhex, '\x00') |
| 485 |
self.assertRaises(ValueError, self.type2test.fromhex, '12 \x00 34') |
| 486 |
|
| 487 |
# For odd number of character(s) |
| 488 |
for value in ("a", "aaa", "deadbee"): |
| 489 |
with self.assertRaises(ValueError) as cm: |
| 490 |
self.type2test.fromhex(value) |
| 491 |
self.assertIn("fromhex() arg must contain an even number of hexadecimal digits", str(cm.exception)) |
| 492 |
for value, position in (("a ", 1), (" aa a ", 5), (" aa a a ", 5)): |
| 493 |
with self.assertRaises(ValueError) as cm: |
| 494 |
self.type2test.fromhex(value) |
| 495 |
self.assertIn(f"non-hexadecimal number found in fromhex() arg at position {position}", str(cm.exception)) |
| 496 |
|
| 497 |
for data, pos in ( |
| 498 |
# invalid first hexadecimal character |
| 499 |
('12 x4 56', 3), |
| 500 |
# invalid second hexadecimal character |
| 501 |
('12 3x 56', 4), |
| 502 |
# two invalid hexadecimal characters |
| 503 |
('12 xy 56', 3), |
| 504 |
# test non-ASCII string |
| 505 |
('12 3\xff 56', 4), |
| 506 |
): |
| 507 |
with self.assertRaises(ValueError) as cm: |
| 508 |
self.type2test.fromhex(data) |
| 509 |
self.assertIn('at position %s' % pos, str(cm.exception)) |
| 510 |
|
| 511 |
def test_hex(self): |
| 512 |
self.assertRaises(TypeError, self.type2test.hex) |
| 513 |
self.assertRaises(TypeError, self.type2test.hex, 1) |
| 514 |
self.assertEqual(self.type2test(b"").hex(), "") |
| 515 |
self.assertEqual(bytearray([0x1a, 0x2b, 0x30]).hex(), '1a2b30') |
| 516 |
self.assertEqual(self.type2test(b"\x1a\x2b\x30").hex(), '1a2b30') |
| 517 |
self.assertEqual(memoryview(b"\x1a\x2b\x30").hex(), '1a2b30') |
| 518 |
|
| 519 |
def test_hex_separator_basics(self): |
| 520 |
three_bytes = self.type2test(b'\xb9\x01\xef') |
| 521 |
self.assertEqual(three_bytes.hex(), 'b901ef') |
| 522 |
with self.assertRaises(ValueError): |
| 523 |
three_bytes.hex('') |
| 524 |
with self.assertRaises(ValueError): |
| 525 |
three_bytes.hex('xx') |
| 526 |
self.assertEqual(three_bytes.hex(':', 0), 'b901ef') |
| 527 |
with self.assertRaises(TypeError): |
| 528 |
three_bytes.hex(None, 0) |
| 529 |
with self.assertRaises(ValueError): |
| 530 |
three_bytes.hex('\xff') |
| 531 |
with self.assertRaises(ValueError): |
| 532 |
three_bytes.hex(b'\xff') |
| 533 |
with self.assertRaises(ValueError): |
| 534 |
three_bytes.hex(b'\x80') |
| 535 |
with self.assertRaises(ValueError): |
| 536 |
three_bytes.hex(chr(0x100)) |
| 537 |
self.assertEqual(three_bytes.hex(':', 0), 'b901ef') |
| 538 |
self.assertEqual(three_bytes.hex(b'\x00'), 'b9\x0001\x00ef') |
| 539 |
self.assertEqual(three_bytes.hex('\x00'), 'b9\x0001\x00ef') |
| 540 |
self.assertEqual(three_bytes.hex(b'\x7f'), 'b9\x7f01\x7fef') |
| 541 |
self.assertEqual(three_bytes.hex('\x7f'), 'b9\x7f01\x7fef') |
| 542 |
self.assertEqual(three_bytes.hex(':', 3), 'b901ef') |
| 543 |
self.assertEqual(three_bytes.hex(':', 4), 'b901ef') |
| 544 |
self.assertEqual(three_bytes.hex(':', -4), 'b901ef') |
| 545 |
self.assertEqual(three_bytes.hex(':'), 'b9:01:ef') |
| 546 |
self.assertEqual(three_bytes.hex(b'$'), 'b9$01$ef') |
| 547 |
self.assertEqual(three_bytes.hex(':', 1), 'b9:01:ef') |
| 548 |
self.assertEqual(three_bytes.hex(':', -1), 'b9:01:ef') |
| 549 |
self.assertEqual(three_bytes.hex(':', 2), 'b9:01ef') |
| 550 |
self.assertEqual(three_bytes.hex(':', 1), 'b9:01:ef') |
| 551 |
self.assertEqual(three_bytes.hex('*', -2), 'b901*ef') |
| 552 |
self.assertEqual(three_bytes.hex(sep=':', bytes_per_sep=2), 'b9:01ef') |
| 553 |
self.assertEqual(three_bytes.hex(sep='*', bytes_per_sep=-2), 'b901*ef') |
| 554 |
for bytes_per_sep in 3, -3, 2**31-1, -(2**31-1): |
| 555 |
with self.subTest(bytes_per_sep=bytes_per_sep): |
| 556 |
self.assertEqual(three_bytes.hex(':', bytes_per_sep), 'b901ef') |
| 557 |
for bytes_per_sep in 2**31, -2**31, 2**1000, -2**1000: |
| 558 |
with self.subTest(bytes_per_sep=bytes_per_sep): |
| 559 |
try: |
| 560 |
self.assertEqual(three_bytes.hex(':', bytes_per_sep), 'b901ef') |
| 561 |
except OverflowError: |
| 562 |
pass |
| 563 |
|
| 564 |
value = b'{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000' |
| 565 |
self.assertEqual(value.hex('.', 8), '7b7305000000776f.726c646902000000.730500000068656c.6c6f690100000030') |
| 566 |
|
| 567 |
def test_hex_separator_five_bytes(self): |
| 568 |
five_bytes = self.type2test(range(90,95)) |
| 569 |
self.assertEqual(five_bytes.hex(), '5a5b5c5d5e') |
| 570 |
|
| 571 |
def test_hex_separator_six_bytes(self): |
| 572 |
six_bytes = self.type2test(x*3 for x in range(1, 7)) |
| 573 |
self.assertEqual(six_bytes.hex(), '0306090c0f12') |
| 574 |
self.assertEqual(six_bytes.hex('.', 1), '03.06.09.0c.0f.12') |
| 575 |
self.assertEqual(six_bytes.hex(' ', 2), '0306 090c 0f12') |
| 576 |
self.assertEqual(six_bytes.hex('-', 3), '030609-0c0f12') |
| 577 |
self.assertEqual(six_bytes.hex(':', 4), '0306:090c0f12') |
| 578 |
self.assertEqual(six_bytes.hex(':', 5), '03:06090c0f12') |
| 579 |
self.assertEqual(six_bytes.hex(':', 6), '0306090c0f12') |
| 580 |
self.assertEqual(six_bytes.hex(':', 95), '0306090c0f12') |
| 581 |
self.assertEqual(six_bytes.hex('_', -3), '030609_0c0f12') |
| 582 |
self.assertEqual(six_bytes.hex(':', -4), '0306090c:0f12') |
| 583 |
self.assertEqual(six_bytes.hex(b'@', -5), '0306090c0f@12') |
| 584 |
self.assertEqual(six_bytes.hex(':', -6), '0306090c0f12') |
| 585 |
self.assertEqual(six_bytes.hex(' ', -95), '0306090c0f12') |
| 586 |
|
| 587 |
def test_join(self): |
| 588 |
self.assertEqual(self.type2test(b"").join([]), b"") |
| 589 |
self.assertEqual(self.type2test(b"").join([b""]), b"") |
| 590 |
for lst in [[b"abc"], [b"a", b"bc"], [b"ab", b"c"], [b"a", b"b", b"c"]]: |
| 591 |
lst = list(map(self.type2test, lst)) |
| 592 |
self.assertEqual(self.type2test(b"").join(lst), b"abc") |
| 593 |
self.assertEqual(self.type2test(b"").join(tuple(lst)), b"abc") |
| 594 |
self.assertEqual(self.type2test(b"").join(iter(lst)), b"abc") |
| 595 |
dot_join = self.type2test(b".:").join |
| 596 |
self.assertEqual(dot_join([b"ab", b"cd"]), b"ab.:cd") |
| 597 |
self.assertEqual(dot_join([memoryview(b"ab"), b"cd"]), b"ab.:cd") |
| 598 |
self.assertEqual(dot_join([b"ab", memoryview(b"cd")]), b"ab.:cd") |
| 599 |
self.assertEqual(dot_join([bytearray(b"ab"), b"cd"]), b"ab.:cd") |
| 600 |
self.assertEqual(dot_join([b"ab", bytearray(b"cd")]), b"ab.:cd") |
| 601 |
# Stress it with many items |
| 602 |
seq = [b"abc"] * 100000 |
| 603 |
expected = b"abc" + b".:abc" * 99999 |
| 604 |
self.assertEqual(dot_join(seq), expected) |
| 605 |
# Stress test with empty separator |
| 606 |
seq = [b"abc"] * 100000 |
| 607 |
expected = b"abc" * 100000 |
| 608 |
self.assertEqual(self.type2test(b"").join(seq), expected) |
| 609 |
self.assertRaises(TypeError, self.type2test(b" ").join, None) |
| 610 |
# Error handling and cleanup when some item in the middle of the |
| 611 |
# sequence has the wrong type. |
| 612 |
with self.assertRaises(TypeError): |
| 613 |
dot_join([bytearray(b"ab"), "cd", b"ef"]) |
| 614 |
with self.assertRaises(TypeError): |
| 615 |
dot_join([memoryview(b"ab"), "cd", b"ef"]) |
| 616 |
|
| 617 |
def test_count(self): |
| 618 |
b = self.type2test(b'mississippi') |
| 619 |
i = 105 |
| 620 |
p = 112 |
| 621 |
w = 119 |
| 622 |
|
| 623 |
self.assertEqual(b.count(b'i'), 4) |
| 624 |
self.assertEqual(b.count(b'ss'), 2) |
| 625 |
self.assertEqual(b.count(b'w'), 0) |
| 626 |
|
| 627 |
self.assertEqual(b.count(i), 4) |
| 628 |
self.assertEqual(b.count(w), 0) |
| 629 |
|
| 630 |
self.assertEqual(b.count(b'i', 6), 2) |
| 631 |
self.assertEqual(b.count(b'p', 6), 2) |
| 632 |
self.assertEqual(b.count(b'i', 1, 3), 1) |
| 633 |
self.assertEqual(b.count(b'p', 7, 9), 1) |
| 634 |
|
| 635 |
self.assertEqual(b.count(i, 6), 2) |
| 636 |
self.assertEqual(b.count(p, 6), 2) |
| 637 |
self.assertEqual(b.count(i, 1, 3), 1) |
| 638 |
self.assertEqual(b.count(p, 7, 9), 1) |
| 639 |
|
| 640 |
def test_startswith(self): |
| 641 |
b = self.type2test(b'hello') |
| 642 |
self.assertFalse(self.type2test().startswith(b"anything")) |
| 643 |
self.assertTrue(b.startswith(b"hello")) |
| 644 |
self.assertTrue(b.startswith(b"hel")) |
| 645 |
self.assertTrue(b.startswith(b"h")) |
| 646 |
self.assertFalse(b.startswith(b"hellow")) |
| 647 |
self.assertFalse(b.startswith(b"ha")) |
| 648 |
with self.assertRaises(TypeError) as cm: |
| 649 |
b.startswith([b'h']) |
| 650 |
exc = str(cm.exception) |
| 651 |
self.assertIn('bytes', exc) |
| 652 |
self.assertIn('tuple', exc) |
| 653 |
|
| 654 |
def test_endswith(self): |
| 655 |
b = self.type2test(b'hello') |
| 656 |
self.assertFalse(bytearray().endswith(b"anything")) |
| 657 |
self.assertTrue(b.endswith(b"hello")) |
| 658 |
self.assertTrue(b.endswith(b"llo")) |
| 659 |
self.assertTrue(b.endswith(b"o")) |
| 660 |
self.assertFalse(b.endswith(b"whello")) |
| 661 |
self.assertFalse(b.endswith(b"no")) |
| 662 |
with self.assertRaises(TypeError) as cm: |
| 663 |
b.endswith([b'o']) |
| 664 |
exc = str(cm.exception) |
| 665 |
self.assertIn('bytes', exc) |
| 666 |
self.assertIn('tuple', exc) |
| 667 |
|
| 668 |
def test_find(self): |
| 669 |
b = self.type2test(b'mississippi') |
| 670 |
i = 105 |
| 671 |
w = 119 |
| 672 |
|
| 673 |
self.assertEqual(b.find(b'ss'), 2) |
| 674 |
self.assertEqual(b.find(b'w'), -1) |
| 675 |
self.assertEqual(b.find(b'mississippian'), -1) |
| 676 |
|
| 677 |
self.assertEqual(b.find(i), 1) |
| 678 |
self.assertEqual(b.find(w), -1) |
| 679 |
|
| 680 |
self.assertEqual(b.find(b'ss', 3), 5) |
| 681 |
self.assertEqual(b.find(b'ss', 1, 7), 2) |
| 682 |
self.assertEqual(b.find(b'ss', 1, 3), -1) |
| 683 |
|
| 684 |
self.assertEqual(b.find(i, 6), 7) |
| 685 |
self.assertEqual(b.find(i, 1, 3), 1) |
| 686 |
self.assertEqual(b.find(w, 1, 3), -1) |
| 687 |
|
| 688 |
for index in (-1, 256, sys.maxsize + 1): |
| 689 |
self.assertRaisesRegex( |
| 690 |
ValueError, r'byte must be in range\(0, 256\)', |
| 691 |
b.find, index) |
| 692 |
|
| 693 |
def test_rfind(self): |
| 694 |
b = self.type2test(b'mississippi') |
| 695 |
i = 105 |
| 696 |
w = 119 |
| 697 |
|
| 698 |
self.assertEqual(b.rfind(b'ss'), 5) |
| 699 |
self.assertEqual(b.rfind(b'w'), -1) |
| 700 |
self.assertEqual(b.rfind(b'mississippian'), -1) |
| 701 |
|
| 702 |
self.assertEqual(b.rfind(i), 10) |
| 703 |
self.assertEqual(b.rfind(w), -1) |
| 704 |
|
| 705 |
self.assertEqual(b.rfind(b'ss', 3), 5) |
| 706 |
self.assertEqual(b.rfind(b'ss', 0, 6), 2) |
| 707 |
|
| 708 |
self.assertEqual(b.rfind(i, 1, 3), 1) |
| 709 |
self.assertEqual(b.rfind(i, 3, 9), 7) |
| 710 |
self.assertEqual(b.rfind(w, 1, 3), -1) |
| 711 |
|
| 712 |
def test_index(self): |
| 713 |
b = self.type2test(b'mississippi') |
| 714 |
i = 105 |
| 715 |
w = 119 |
| 716 |
|
| 717 |
self.assertEqual(b.index(b'ss'), 2) |
| 718 |
self.assertRaises(ValueError, b.index, b'w') |
| 719 |
self.assertRaises(ValueError, b.index, b'mississippian') |
| 720 |
|
| 721 |
self.assertEqual(b.index(i), 1) |
| 722 |
self.assertRaises(ValueError, b.index, w) |
| 723 |
|
| 724 |
self.assertEqual(b.index(b'ss', 3), 5) |
| 725 |
self.assertEqual(b.index(b'ss', 1, 7), 2) |
| 726 |
self.assertRaises(ValueError, b.index, b'ss', 1, 3) |
| 727 |
|
| 728 |
self.assertEqual(b.index(i, 6), 7) |
| 729 |
self.assertEqual(b.index(i, 1, 3), 1) |
| 730 |
self.assertRaises(ValueError, b.index, w, 1, 3) |
| 731 |
|
| 732 |
def test_rindex(self): |
| 733 |
b = self.type2test(b'mississippi') |
| 734 |
i = 105 |
| 735 |
w = 119 |
| 736 |
|
| 737 |
self.assertEqual(b.rindex(b'ss'), 5) |
| 738 |
self.assertRaises(ValueError, b.rindex, b'w') |
| 739 |
self.assertRaises(ValueError, b.rindex, b'mississippian') |
| 740 |
|
| 741 |
self.assertEqual(b.rindex(i), 10) |
| 742 |
self.assertRaises(ValueError, b.rindex, w) |
| 743 |
|
| 744 |
self.assertEqual(b.rindex(b'ss', 3), 5) |
| 745 |
self.assertEqual(b.rindex(b'ss', 0, 6), 2) |
| 746 |
|
| 747 |
self.assertEqual(b.rindex(i, 1, 3), 1) |
| 748 |
self.assertEqual(b.rindex(i, 3, 9), 7) |
| 749 |
self.assertRaises(ValueError, b.rindex, w, 1, 3) |
| 750 |
|
| 751 |
def test_mod(self): |
| 752 |
b = self.type2test(b'hello, %b!') |
| 753 |
orig = b |
| 754 |
b = b % b'world' |
| 755 |
self.assertEqual(b, b'hello, world!') |
| 756 |
self.assertEqual(orig, b'hello, %b!') |
| 757 |
self.assertFalse(b is orig) |
| 758 |
b = self.type2test(b'%s / 100 = %d%%') |
| 759 |
a = b % (b'seventy-nine', 79) |
| 760 |
self.assertEqual(a, b'seventy-nine / 100 = 79%') |
| 761 |
self.assertIs(type(a), self.type2test) |
| 762 |
# issue 29714 |
| 763 |
b = self.type2test(b'hello,\x00%b!') |
| 764 |
b = b % b'world' |
| 765 |
self.assertEqual(b, b'hello,\x00world!') |
| 766 |
self.assertIs(type(b), self.type2test) |
| 767 |
|
| 768 |
def check(fmt, vals, result): |
| 769 |
b = self.type2test(fmt) |
| 770 |
b = b % vals |
| 771 |
self.assertEqual(b, result) |
| 772 |
self.assertIs(type(b), self.type2test) |
| 773 |
|
| 774 |
# A set of tests adapted from test_unicode:UnicodeTest.test_formatting |
| 775 |
check(b'...%(foo)b...', {b'foo':b"abc"}, b'...abc...') |
| 776 |
check(b'...%(f(o)o)b...', {b'f(o)o':b"abc", b'foo':b'bar'}, b'...abc...') |
| 777 |
check(b'...%(foo)b...', {b'foo':b"abc",b'def':123}, b'...abc...') |
| 778 |
check(b'%*b', (5, b'abc',), b' abc') |
| 779 |
check(b'%*b', (-5, b'abc',), b'abc ') |
| 780 |
check(b'%*.*b', (5, 2, b'abc',), b' ab') |
| 781 |
check(b'%*.*b', (5, 3, b'abc',), b' abc') |
| 782 |
check(b'%i %*.*b', (10, 5, 3, b'abc',), b'10 abc') |
| 783 |
check(b'%i%b %*.*b', (10, b'3', 5, 3, b'abc',), b'103 abc') |
| 784 |
check(b'%c', b'a', b'a') |
| 785 |
|
| 786 |
class PseudoFloat: |
| 787 |
def __init__(self, value): |
| 788 |
self.value = float(value) |
| 789 |
def __int__(self): |
| 790 |
return int(self.value) |
| 791 |
|
| 792 |
pi = PseudoFloat(3.1415) |
| 793 |
|
| 794 |
exceptions_params = [ |
| 795 |
('%x format: an integer is required, not float', b'%x', 3.14), |
| 796 |
('%X format: an integer is required, not float', b'%X', 2.11), |
| 797 |
('%o format: an integer is required, not float', b'%o', 1.79), |
| 798 |
('%x format: an integer is required, not PseudoFloat', b'%x', pi), |
| 799 |
('%x format: an integer is required, not complex', b'%x', 3j), |
| 800 |
('%X format: an integer is required, not complex', b'%X', 2j), |
| 801 |
('%o format: an integer is required, not complex', b'%o', 1j), |
| 802 |
('%u format: a real number is required, not complex', b'%u', 3j), |
| 803 |
('%i format: a real number is required, not complex', b'%i', 2j), |
| 804 |
('%d format: a real number is required, not complex', b'%d', 2j), |
| 805 |
( |
| 806 |
r'%c requires an integer in range\(256\)' |
| 807 |
r' or a single byte, not .*\.PseudoFloat', |
| 808 |
b'%c', pi |
| 809 |
), |
| 810 |
] |
| 811 |
|
| 812 |
for msg, format_bytes, value in exceptions_params: |
| 813 |
with self.assertRaisesRegex(TypeError, msg): |
| 814 |
operator.mod(format_bytes, value) |
| 815 |
|
| 816 |
def test_memory_leak_gh_140939(self): |
| 817 |
# gh-140939: MemoryError is raised without leaking |
| 818 |
_testcapi = import_helper.import_module('_testcapi') |
| 819 |
with self.assertRaises(MemoryError): |
| 820 |
b = self.type2test(b'%*b') |
| 821 |
b % (_testcapi.PY_SSIZE_T_MAX, b'abc') |
| 822 |
|
| 823 |
def test_imod(self): |
| 824 |
b = self.type2test(b'hello, %b!') |
| 825 |
orig = b |
| 826 |
b %= b'world' |
| 827 |
self.assertEqual(b, b'hello, world!') |
| 828 |
self.assertEqual(orig, b'hello, %b!') |
| 829 |
self.assertFalse(b is orig) |
| 830 |
b = self.type2test(b'%s / 100 = %d%%') |
| 831 |
b %= (b'seventy-nine', 79) |
| 832 |
self.assertEqual(b, b'seventy-nine / 100 = 79%') |
| 833 |
self.assertIs(type(b), self.type2test) |
| 834 |
# issue 29714 |
| 835 |
b = self.type2test(b'hello,\x00%b!') |
| 836 |
b %= b'world' |
| 837 |
self.assertEqual(b, b'hello,\x00world!') |
| 838 |
self.assertIs(type(b), self.type2test) |
| 839 |
|
| 840 |
def test_rmod(self): |
| 841 |
with self.assertRaises(TypeError): |
| 842 |
object() % self.type2test(b'abc') |
| 843 |
self.assertIs(self.type2test(b'abc').__rmod__('%r'), NotImplemented) |
| 844 |
|
| 845 |
def test_replace(self): |
| 846 |
b = self.type2test(b'mississippi') |
| 847 |
self.assertEqual(b.replace(b'i', b'a'), b'massassappa') |
| 848 |
self.assertEqual(b.replace(b'ss', b'x'), b'mixixippi') |
| 849 |
|
| 850 |
def test_replace_int_error(self): |
| 851 |
self.assertRaises(TypeError, self.type2test(b'a b').replace, 32, b'') |
| 852 |
|
| 853 |
def test_split_string_error(self): |
| 854 |
self.assertRaises(TypeError, self.type2test(b'a b').split, ' ') |
| 855 |
self.assertRaises(TypeError, self.type2test(b'a b').rsplit, ' ') |
| 856 |
|
| 857 |
def test_split_int_error(self): |
| 858 |
self.assertRaises(TypeError, self.type2test(b'a b').split, 32) |
| 859 |
self.assertRaises(TypeError, self.type2test(b'a b').rsplit, 32) |
| 860 |
|
| 861 |
def test_split_unicodewhitespace(self): |
| 862 |
for b in (b'a\x1Cb', b'a\x1Db', b'a\x1Eb', b'a\x1Fb'): |
| 863 |
b = self.type2test(b) |
| 864 |
self.assertEqual(b.split(), [b]) |
| 865 |
b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F") |
| 866 |
self.assertEqual(b.split(), [b'\x1c\x1d\x1e\x1f']) |
| 867 |
|
| 868 |
def test_rsplit_unicodewhitespace(self): |
| 869 |
b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F") |
| 870 |
self.assertEqual(b.rsplit(), [b'\x1c\x1d\x1e\x1f']) |
| 871 |
|
| 872 |
def test_partition(self): |
| 873 |
b = self.type2test(b'mississippi') |
| 874 |
self.assertEqual(b.partition(b'ss'), (b'mi', b'ss', b'issippi')) |
| 875 |
self.assertEqual(b.partition(b'w'), (b'mississippi', b'', b'')) |
| 876 |
|
| 877 |
def test_rpartition(self): |
| 878 |
b = self.type2test(b'mississippi') |
| 879 |
self.assertEqual(b.rpartition(b'ss'), (b'missi', b'ss', b'ippi')) |
| 880 |
self.assertEqual(b.rpartition(b'i'), (b'mississipp', b'i', b'')) |
| 881 |
self.assertEqual(b.rpartition(b'w'), (b'', b'', b'mississippi')) |
| 882 |
|
| 883 |
def test_partition_string_error(self): |
| 884 |
self.assertRaises(TypeError, self.type2test(b'a b').partition, ' ') |
| 885 |
self.assertRaises(TypeError, self.type2test(b'a b').rpartition, ' ') |
| 886 |
|
| 887 |
def test_partition_int_error(self): |
| 888 |
self.assertRaises(TypeError, self.type2test(b'a b').partition, 32) |
| 889 |
self.assertRaises(TypeError, self.type2test(b'a b').rpartition, 32) |
| 890 |
|
| 891 |
def test_pickling(self): |
| 892 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 893 |
for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0": |
| 894 |
b = self.type2test(b) |
| 895 |
ps = pickle.dumps(b, proto) |
| 896 |
q = pickle.loads(ps) |
| 897 |
self.assertEqual(b, q) |
| 898 |
|
| 899 |
def test_iterator_pickling(self): |
| 900 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 901 |
for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0": |
| 902 |
it = itorg = iter(self.type2test(b)) |
| 903 |
data = list(self.type2test(b)) |
| 904 |
d = pickle.dumps(it, proto) |
| 905 |
it = pickle.loads(d) |
| 906 |
self.assertEqual(type(itorg), type(it)) |
| 907 |
self.assertEqual(list(it), data) |
| 908 |
|
| 909 |
it = pickle.loads(d) |
| 910 |
if not b: |
| 911 |
continue |
| 912 |
next(it) |
| 913 |
d = pickle.dumps(it, proto) |
| 914 |
it = pickle.loads(d) |
| 915 |
self.assertEqual(list(it), data[1:]) |
| 916 |
|
| 917 |
def test_strip_bytearray(self): |
| 918 |
self.assertEqual(self.type2test(b'abc').strip(memoryview(b'ac')), b'b') |
| 919 |
self.assertEqual(self.type2test(b'abc').lstrip(memoryview(b'ac')), b'bc') |
| 920 |
self.assertEqual(self.type2test(b'abc').rstrip(memoryview(b'ac')), b'ab') |
| 921 |
|
| 922 |
def test_strip_string_error(self): |
| 923 |
self.assertRaises(TypeError, self.type2test(b'abc').strip, 'ac') |
| 924 |
self.assertRaises(TypeError, self.type2test(b'abc').lstrip, 'ac') |
| 925 |
self.assertRaises(TypeError, self.type2test(b'abc').rstrip, 'ac') |
| 926 |
|
| 927 |
def test_strip_int_error(self): |
| 928 |
self.assertRaises(TypeError, self.type2test(b' abc ').strip, 32) |
| 929 |
self.assertRaises(TypeError, self.type2test(b' abc ').lstrip, 32) |
| 930 |
self.assertRaises(TypeError, self.type2test(b' abc ').rstrip, 32) |
| 931 |
|
| 932 |
def test_center(self): |
| 933 |
# Fill character can be either bytes or bytearray (issue 12380) |
| 934 |
b = self.type2test(b'abc') |
| 935 |
for fill_type in (bytes, bytearray): |
| 936 |
self.assertEqual(b.center(7, fill_type(b'-')), |
| 937 |
self.type2test(b'--abc--')) |
| 938 |
|
| 939 |
def test_ljust(self): |
| 940 |
# Fill character can be either bytes or bytearray (issue 12380) |
| 941 |
b = self.type2test(b'abc') |
| 942 |
for fill_type in (bytes, bytearray): |
| 943 |
self.assertEqual(b.ljust(7, fill_type(b'-')), |
| 944 |
self.type2test(b'abc----')) |
| 945 |
|
| 946 |
def test_rjust(self): |
| 947 |
# Fill character can be either bytes or bytearray (issue 12380) |
| 948 |
b = self.type2test(b'abc') |
| 949 |
for fill_type in (bytes, bytearray): |
| 950 |
self.assertEqual(b.rjust(7, fill_type(b'-')), |
| 951 |
self.type2test(b'----abc')) |
| 952 |
|
| 953 |
def test_xjust_int_error(self): |
| 954 |
self.assertRaises(TypeError, self.type2test(b'abc').center, 7, 32) |
| 955 |
self.assertRaises(TypeError, self.type2test(b'abc').ljust, 7, 32) |
| 956 |
self.assertRaises(TypeError, self.type2test(b'abc').rjust, 7, 32) |
| 957 |
|
| 958 |
def test_ord(self): |
| 959 |
b = self.type2test(b'\0A\x7f\x80\xff') |
| 960 |
self.assertEqual([ord(b[i:i+1]) for i in range(len(b))], |
| 961 |
[0, 65, 127, 128, 255]) |
| 962 |
|
| 963 |
def test_maketrans(self): |
| 964 |
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' |
| 965 |
self.assertEqual(self.type2test.maketrans(b'abc', b'xyz'), transtable) |
| 966 |
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' |
| 967 |
self.assertEqual(self.type2test.maketrans(b'\375\376\377', b'xyz'), transtable) |
| 968 |
self.assertRaises(ValueError, self.type2test.maketrans, b'abc', b'xyzq') |
| 969 |
self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 'def') |
| 970 |
|
| 971 |
def test_none_arguments(self): |
| 972 |
# issue 11828 |
| 973 |
b = self.type2test(b'hello') |
| 974 |
l = self.type2test(b'l') |
| 975 |
h = self.type2test(b'h') |
| 976 |
x = self.type2test(b'x') |
| 977 |
o = self.type2test(b'o') |
| 978 |
|
| 979 |
self.assertEqual(2, b.find(l, None)) |
| 980 |
self.assertEqual(3, b.find(l, -2, None)) |
| 981 |
self.assertEqual(2, b.find(l, None, -2)) |
| 982 |
self.assertEqual(0, b.find(h, None, None)) |
| 983 |
|
| 984 |
self.assertEqual(3, b.rfind(l, None)) |
| 985 |
self.assertEqual(3, b.rfind(l, -2, None)) |
| 986 |
self.assertEqual(2, b.rfind(l, None, -2)) |
| 987 |
self.assertEqual(0, b.rfind(h, None, None)) |
| 988 |
|
| 989 |
self.assertEqual(2, b.index(l, None)) |
| 990 |
self.assertEqual(3, b.index(l, -2, None)) |
| 991 |
self.assertEqual(2, b.index(l, None, -2)) |
| 992 |
self.assertEqual(0, b.index(h, None, None)) |
| 993 |
|
| 994 |
self.assertEqual(3, b.rindex(l, None)) |
| 995 |
self.assertEqual(3, b.rindex(l, -2, None)) |
| 996 |
self.assertEqual(2, b.rindex(l, None, -2)) |
| 997 |
self.assertEqual(0, b.rindex(h, None, None)) |
| 998 |
|
| 999 |
self.assertEqual(2, b.count(l, None)) |
| 1000 |
self.assertEqual(1, b.count(l, -2, None)) |
| 1001 |
self.assertEqual(1, b.count(l, None, -2)) |
| 1002 |
self.assertEqual(0, b.count(x, None, None)) |
| 1003 |
|
| 1004 |
self.assertEqual(True, b.endswith(o, None)) |
| 1005 |
self.assertEqual(True, b.endswith(o, -2, None)) |
| 1006 |
self.assertEqual(True, b.endswith(l, None, -2)) |
| 1007 |
self.assertEqual(False, b.endswith(x, None, None)) |
| 1008 |
|
| 1009 |
self.assertEqual(True, b.startswith(h, None)) |
| 1010 |
self.assertEqual(True, b.startswith(l, -2, None)) |
| 1011 |
self.assertEqual(True, b.startswith(h, None, -2)) |
| 1012 |
self.assertEqual(False, b.startswith(x, None, None)) |
| 1013 |
|
| 1014 |
def test_integer_arguments_out_of_byte_range(self): |
| 1015 |
b = self.type2test(b'hello') |
| 1016 |
|
| 1017 |
for method in (b.count, b.find, b.index, b.rfind, b.rindex): |
| 1018 |
self.assertRaises(ValueError, method, -1) |
| 1019 |
self.assertRaises(ValueError, method, 256) |
| 1020 |
self.assertRaises(ValueError, method, 9999) |
| 1021 |
|
| 1022 |
def test_find_etc_raise_correct_error_messages(self): |
| 1023 |
# issue 11828 |
| 1024 |
b = self.type2test(b'hello') |
| 1025 |
x = self.type2test(b'x') |
| 1026 |
self.assertRaisesRegex(TypeError, r'\bfind\b', b.find, |
| 1027 |
x, None, None, None) |
| 1028 |
self.assertRaisesRegex(TypeError, r'\brfind\b', b.rfind, |
| 1029 |
x, None, None, None) |
| 1030 |
self.assertRaisesRegex(TypeError, r'\bindex\b', b.index, |
| 1031 |
x, None, None, None) |
| 1032 |
self.assertRaisesRegex(TypeError, r'\brindex\b', b.rindex, |
| 1033 |
x, None, None, None) |
| 1034 |
self.assertRaisesRegex(TypeError, r'\bcount\b', b.count, |
| 1035 |
x, None, None, None) |
| 1036 |
self.assertRaisesRegex(TypeError, r'\bstartswith\b', b.startswith, |
| 1037 |
x, None, None, None) |
| 1038 |
self.assertRaisesRegex(TypeError, r'\bendswith\b', b.endswith, |
| 1039 |
x, None, None, None) |
| 1040 |
|
| 1041 |
def test_free_after_iterating(self): |
| 1042 |
test.support.check_free_after_iterating(self, iter, self.type2test) |
| 1043 |
test.support.check_free_after_iterating(self, reversed, self.type2test) |
| 1044 |
|
| 1045 |
def test_translate(self): |
| 1046 |
b = self.type2test(b'hello') |
| 1047 |
rosetta = bytearray(range(256)) |
| 1048 |
rosetta[ord('o')] = ord('e') |
| 1049 |
|
| 1050 |
self.assertRaises(TypeError, b.translate) |
| 1051 |
self.assertRaises(TypeError, b.translate, None, None) |
| 1052 |
self.assertRaises(ValueError, b.translate, bytes(range(255))) |
| 1053 |
|
| 1054 |
c = b.translate(rosetta, b'hello') |
| 1055 |
self.assertEqual(b, b'hello') |
| 1056 |
self.assertIsInstance(c, self.type2test) |
| 1057 |
|
| 1058 |
c = b.translate(rosetta) |
| 1059 |
d = b.translate(rosetta, b'') |
| 1060 |
self.assertEqual(c, d) |
| 1061 |
self.assertEqual(c, b'helle') |
| 1062 |
|
| 1063 |
c = b.translate(rosetta, b'l') |
| 1064 |
self.assertEqual(c, b'hee') |
| 1065 |
c = b.translate(None, b'e') |
| 1066 |
self.assertEqual(c, b'hllo') |
| 1067 |
|
| 1068 |
# test delete as a keyword argument |
| 1069 |
c = b.translate(rosetta, delete=b'') |
| 1070 |
self.assertEqual(c, b'helle') |
| 1071 |
c = b.translate(rosetta, delete=b'l') |
| 1072 |
self.assertEqual(c, b'hee') |
| 1073 |
c = b.translate(None, delete=b'e') |
| 1074 |
self.assertEqual(c, b'hllo') |
| 1075 |
|
| 1076 |
def test_sq_item(self): |
| 1077 |
_testlimitedcapi = import_helper.import_module('_testlimitedcapi') |
| 1078 |
obj = self.type2test((42,)) |
| 1079 |
with self.assertRaises(IndexError): |
| 1080 |
_testlimitedcapi.sequence_getitem(obj, -2) |
| 1081 |
with self.assertRaises(IndexError): |
| 1082 |
_testlimitedcapi.sequence_getitem(obj, 1) |
| 1083 |
self.assertEqual(_testlimitedcapi.sequence_getitem(obj, 0), 42) |
| 1084 |
|
| 1085 |
|
| 1086 |
class BytesTest(BaseBytesTest, unittest.TestCase): |
| 1087 |
type2test = bytes |
| 1088 |
|
| 1089 |
def test__bytes__(self): |
| 1090 |
foo = b'foo\x00bar' |
| 1091 |
self.assertEqual(foo.__bytes__(), foo) |
| 1092 |
self.assertEqual(type(foo.__bytes__()), self.type2test) |
| 1093 |
|
| 1094 |
class bytes_subclass(bytes): |
| 1095 |
pass |
| 1096 |
|
| 1097 |
bar = bytes_subclass(b'bar\x00foo') |
| 1098 |
self.assertEqual(bar.__bytes__(), bar) |
| 1099 |
self.assertEqual(type(bar.__bytes__()), self.type2test) |
| 1100 |
|
| 1101 |
def test_getitem_error(self): |
| 1102 |
b = b'python' |
| 1103 |
msg = "byte indices must be integers or slices" |
| 1104 |
with self.assertRaisesRegex(TypeError, msg): |
| 1105 |
b['a'] |
| 1106 |
|
| 1107 |
def test_buffer_is_readonly(self): |
| 1108 |
fd = os.open(__file__, os.O_RDONLY) |
| 1109 |
with open(fd, "rb", buffering=0) as f: |
| 1110 |
self.assertRaises(TypeError, f.readinto, b"") |
| 1111 |
|
| 1112 |
def test_custom(self): |
| 1113 |
self.assertEqual(bytes(BytesSubclass(b'abc')), b'abc') |
| 1114 |
self.assertEqual(BytesSubclass(OtherBytesSubclass(b'abc')), |
| 1115 |
BytesSubclass(b'abc')) |
| 1116 |
self.assertEqual(bytes(WithBytes(b'abc')), b'abc') |
| 1117 |
self.assertEqual(BytesSubclass(WithBytes(b'abc')), BytesSubclass(b'abc')) |
| 1118 |
|
| 1119 |
class NoBytes: pass |
| 1120 |
self.assertRaises(TypeError, bytes, NoBytes()) |
| 1121 |
self.assertRaises(TypeError, bytes, WithBytes('abc')) |
| 1122 |
self.assertRaises(TypeError, bytes, WithBytes(None)) |
| 1123 |
class IndexWithBytes: |
| 1124 |
def __bytes__(self): |
| 1125 |
return b'a' |
| 1126 |
def __index__(self): |
| 1127 |
return 42 |
| 1128 |
self.assertEqual(bytes(IndexWithBytes()), b'a') |
| 1129 |
# Issue #25766 |
| 1130 |
class StrWithBytes(str): |
| 1131 |
def __new__(cls, value): |
| 1132 |
self = str.__new__(cls, '\u20ac') |
| 1133 |
self.value = value |
| 1134 |
return self |
| 1135 |
def __bytes__(self): |
| 1136 |
return self.value |
| 1137 |
self.assertEqual(bytes(StrWithBytes(b'abc')), b'abc') |
| 1138 |
self.assertEqual(bytes(StrWithBytes(b'abc'), 'iso8859-15'), b'\xa4') |
| 1139 |
self.assertEqual(bytes(StrWithBytes(BytesSubclass(b'abc'))), b'abc') |
| 1140 |
self.assertEqual(BytesSubclass(StrWithBytes(b'abc')), BytesSubclass(b'abc')) |
| 1141 |
self.assertEqual(BytesSubclass(StrWithBytes(b'abc'), 'iso8859-15'), |
| 1142 |
BytesSubclass(b'\xa4')) |
| 1143 |
self.assertEqual(BytesSubclass(StrWithBytes(BytesSubclass(b'abc'))), |
| 1144 |
BytesSubclass(b'abc')) |
| 1145 |
self.assertEqual(BytesSubclass(StrWithBytes(OtherBytesSubclass(b'abc'))), |
| 1146 |
BytesSubclass(b'abc')) |
| 1147 |
# Issue #24731 |
| 1148 |
self.assertTypedEqual(bytes(WithBytes(BytesSubclass(b'abc'))), BytesSubclass(b'abc')) |
| 1149 |
self.assertTypedEqual(BytesSubclass(WithBytes(BytesSubclass(b'abc'))), |
| 1150 |
BytesSubclass(b'abc')) |
| 1151 |
self.assertTypedEqual(BytesSubclass(WithBytes(OtherBytesSubclass(b'abc'))), |
| 1152 |
BytesSubclass(b'abc')) |
| 1153 |
|
| 1154 |
class BytesWithBytes(bytes): |
| 1155 |
def __new__(cls, value): |
| 1156 |
self = bytes.__new__(cls, b'\xa4') |
| 1157 |
self.value = value |
| 1158 |
return self |
| 1159 |
def __bytes__(self): |
| 1160 |
return self.value |
| 1161 |
self.assertTypedEqual(bytes(BytesWithBytes(b'abc')), b'abc') |
| 1162 |
self.assertTypedEqual(BytesSubclass(BytesWithBytes(b'abc')), |
| 1163 |
BytesSubclass(b'abc')) |
| 1164 |
self.assertTypedEqual(bytes(BytesWithBytes(BytesSubclass(b'abc'))), |
| 1165 |
BytesSubclass(b'abc')) |
| 1166 |
self.assertTypedEqual(BytesSubclass(BytesWithBytes(BytesSubclass(b'abc'))), |
| 1167 |
BytesSubclass(b'abc')) |
| 1168 |
self.assertTypedEqual(BytesSubclass(BytesWithBytes(OtherBytesSubclass(b'abc'))), |
| 1169 |
BytesSubclass(b'abc')) |
| 1170 |
|
| 1171 |
# Test PyBytes_FromFormat() |
| 1172 |
def test_from_format(self): |
| 1173 |
ctypes = import_helper.import_module('ctypes') |
| 1174 |
_testcapi = import_helper.import_module('_testcapi') |
| 1175 |
from ctypes import pythonapi, py_object |
| 1176 |
from ctypes import ( |
| 1177 |
c_int, c_uint, |
| 1178 |
c_long, c_ulong, |
| 1179 |
c_size_t, c_ssize_t, |
| 1180 |
c_char_p) |
| 1181 |
|
| 1182 |
PyBytes_FromFormat = pythonapi.PyBytes_FromFormat |
| 1183 |
PyBytes_FromFormat.argtypes = (c_char_p,) |
| 1184 |
PyBytes_FromFormat.restype = py_object |
| 1185 |
|
| 1186 |
# basic tests |
| 1187 |
self.assertEqual(PyBytes_FromFormat(b'format'), |
| 1188 |
b'format') |
| 1189 |
self.assertEqual(PyBytes_FromFormat(b'Hello %s !', b'world'), |
| 1190 |
b'Hello world !') |
| 1191 |
|
| 1192 |
# test formatters |
| 1193 |
self.assertEqual(PyBytes_FromFormat(b'c=%c', c_int(0)), |
| 1194 |
b'c=\0') |
| 1195 |
self.assertEqual(PyBytes_FromFormat(b'c=%c', c_int(ord('@'))), |
| 1196 |
b'c=@') |
| 1197 |
self.assertEqual(PyBytes_FromFormat(b'c=%c', c_int(255)), |
| 1198 |
b'c=\xff') |
| 1199 |
self.assertEqual(PyBytes_FromFormat(b'd=%d ld=%ld zd=%zd', |
| 1200 |
c_int(1), c_long(2), |
| 1201 |
c_size_t(3)), |
| 1202 |
b'd=1 ld=2 zd=3') |
| 1203 |
self.assertEqual(PyBytes_FromFormat(b'd=%d ld=%ld zd=%zd', |
| 1204 |
c_int(-1), c_long(-2), |
| 1205 |
c_size_t(-3)), |
| 1206 |
b'd=-1 ld=-2 zd=-3') |
| 1207 |
self.assertEqual(PyBytes_FromFormat(b'u=%u lu=%lu zu=%zu', |
| 1208 |
c_uint(123), c_ulong(456), |
| 1209 |
c_size_t(789)), |
| 1210 |
b'u=123 lu=456 zu=789') |
| 1211 |
self.assertEqual(PyBytes_FromFormat(b'i=%i', c_int(123)), |
| 1212 |
b'i=123') |
| 1213 |
self.assertEqual(PyBytes_FromFormat(b'i=%i', c_int(-123)), |
| 1214 |
b'i=-123') |
| 1215 |
self.assertEqual(PyBytes_FromFormat(b'x=%x', c_int(0xabc)), |
| 1216 |
b'x=abc') |
| 1217 |
|
| 1218 |
sizeof_ptr = ctypes.sizeof(c_char_p) |
| 1219 |
|
| 1220 |
if os.name == 'nt': |
| 1221 |
# Windows (MSCRT) |
| 1222 |
ptr_format = '0x%0{}X'.format(2 * sizeof_ptr) |
| 1223 |
def ptr_formatter(ptr): |
| 1224 |
return (ptr_format % ptr) |
| 1225 |
else: |
| 1226 |
# UNIX (glibc) |
| 1227 |
def ptr_formatter(ptr): |
| 1228 |
return '%#x' % ptr |
| 1229 |
|
| 1230 |
ptr = 0xabcdef |
| 1231 |
self.assertEqual(PyBytes_FromFormat(b'ptr=%p', c_char_p(ptr)), |
| 1232 |
('ptr=' + ptr_formatter(ptr)).encode('ascii')) |
| 1233 |
self.assertEqual(PyBytes_FromFormat(b's=%s', c_char_p(b'cstr')), |
| 1234 |
b's=cstr') |
| 1235 |
|
| 1236 |
# test minimum and maximum integer values |
| 1237 |
size_max = c_size_t(-1).value |
| 1238 |
for formatstr, ctypes_type, value, py_formatter in ( |
| 1239 |
(b'%d', c_int, _testcapi.INT_MIN, str), |
| 1240 |
(b'%d', c_int, _testcapi.INT_MAX, str), |
| 1241 |
(b'%ld', c_long, _testcapi.LONG_MIN, str), |
| 1242 |
(b'%ld', c_long, _testcapi.LONG_MAX, str), |
| 1243 |
(b'%lu', c_ulong, _testcapi.ULONG_MAX, str), |
| 1244 |
(b'%zd', c_ssize_t, _testcapi.PY_SSIZE_T_MIN, str), |
| 1245 |
(b'%zd', c_ssize_t, _testcapi.PY_SSIZE_T_MAX, str), |
| 1246 |
(b'%zu', c_size_t, size_max, str), |
| 1247 |
(b'%p', c_char_p, size_max, ptr_formatter), |
| 1248 |
): |
| 1249 |
self.assertEqual(PyBytes_FromFormat(formatstr, ctypes_type(value)), |
| 1250 |
py_formatter(value).encode('ascii')), |
| 1251 |
|
| 1252 |
# width and precision (width is currently ignored) |
| 1253 |
self.assertEqual(PyBytes_FromFormat(b'%5s', b'a'), |
| 1254 |
b'a') |
| 1255 |
self.assertEqual(PyBytes_FromFormat(b'%.3s', b'abcdef'), |
| 1256 |
b'abc') |
| 1257 |
|
| 1258 |
# '%%' formatter |
| 1259 |
self.assertEqual(PyBytes_FromFormat(b'%%'), |
| 1260 |
b'%') |
| 1261 |
self.assertEqual(PyBytes_FromFormat(b'[%%]'), |
| 1262 |
b'[%]') |
| 1263 |
self.assertEqual(PyBytes_FromFormat(b'%%%c', c_int(ord('_'))), |
| 1264 |
b'%_') |
| 1265 |
self.assertEqual(PyBytes_FromFormat(b'%%s'), |
| 1266 |
b'%s') |
| 1267 |
|
| 1268 |
# Invalid formats and partial formatting |
| 1269 |
self.assertEqual(PyBytes_FromFormat(b'%'), b'%') |
| 1270 |
self.assertEqual(PyBytes_FromFormat(b'x=%i y=%', c_int(2), c_int(3)), |
| 1271 |
b'x=2 y=%') |
| 1272 |
|
| 1273 |
# Issue #19969: %c must raise OverflowError for values |
| 1274 |
# not in the range [0; 255] |
| 1275 |
self.assertRaises(OverflowError, |
| 1276 |
PyBytes_FromFormat, b'%c', c_int(-1)) |
| 1277 |
self.assertRaises(OverflowError, |
| 1278 |
PyBytes_FromFormat, b'%c', c_int(256)) |
| 1279 |
|
| 1280 |
# Issue #33817: empty strings |
| 1281 |
self.assertEqual(PyBytes_FromFormat(b''), |
| 1282 |
b'') |
| 1283 |
self.assertEqual(PyBytes_FromFormat(b'%s', b''), |
| 1284 |
b'') |
| 1285 |
|
| 1286 |
def test_bytes_blocking(self): |
| 1287 |
class IterationBlocked(list): |
| 1288 |
__bytes__ = None |
| 1289 |
i = [0, 1, 2, 3] |
| 1290 |
self.assertEqual(bytes(i), b'\x00\x01\x02\x03') |
| 1291 |
self.assertRaises(TypeError, bytes, IterationBlocked(i)) |
| 1292 |
|
| 1293 |
# At least in CPython, because bytes.__new__ and the C API |
| 1294 |
# PyBytes_FromObject have different fallback rules, integer |
| 1295 |
# fallback is handled specially, so test separately. |
| 1296 |
class IntBlocked(int): |
| 1297 |
__bytes__ = None |
| 1298 |
self.assertEqual(bytes(3), b'\0\0\0') |
| 1299 |
self.assertRaises(TypeError, bytes, IntBlocked(3)) |
| 1300 |
|
| 1301 |
# While there is no separately-defined rule for handling bytes |
| 1302 |
# subclasses differently from other buffer-interface classes, |
| 1303 |
# an implementation may well special-case them (as CPython 2.x |
| 1304 |
# str did), so test them separately. |
| 1305 |
class BytesSubclassBlocked(bytes): |
| 1306 |
__bytes__ = None |
| 1307 |
self.assertEqual(bytes(b'ab'), b'ab') |
| 1308 |
self.assertRaises(TypeError, bytes, BytesSubclassBlocked(b'ab')) |
| 1309 |
|
| 1310 |
class BufferBlocked(bytearray): |
| 1311 |
__bytes__ = None |
| 1312 |
ba, bb = bytearray(b'ab'), BufferBlocked(b'ab') |
| 1313 |
self.assertEqual(bytes(ba), b'ab') |
| 1314 |
self.assertRaises(TypeError, bytes, bb) |
| 1315 |
|
| 1316 |
def test_repeat_id_preserving(self): |
| 1317 |
a = b'123abc1@' |
| 1318 |
b = b'456zyx-+' |
| 1319 |
self.assertEqual(id(a), id(a)) |
| 1320 |
self.assertNotEqual(id(a), id(b)) |
| 1321 |
self.assertNotEqual(id(a), id(a * -4)) |
| 1322 |
self.assertNotEqual(id(a), id(a * 0)) |
| 1323 |
self.assertEqual(id(a), id(a * 1)) |
| 1324 |
self.assertEqual(id(a), id(1 * a)) |
| 1325 |
self.assertNotEqual(id(a), id(a * 2)) |
| 1326 |
|
| 1327 |
class SubBytes(bytes): |
| 1328 |
pass |
| 1329 |
|
| 1330 |
s = SubBytes(b'qwerty()') |
| 1331 |
self.assertEqual(id(s), id(s)) |
| 1332 |
self.assertNotEqual(id(s), id(s * -4)) |
| 1333 |
self.assertNotEqual(id(s), id(s * 0)) |
| 1334 |
self.assertNotEqual(id(s), id(s * 1)) |
| 1335 |
self.assertNotEqual(id(s), id(1 * s)) |
| 1336 |
self.assertNotEqual(id(s), id(s * 2)) |
| 1337 |
|
| 1338 |
|
| 1339 |
class ByteArrayTest(BaseBytesTest, unittest.TestCase): |
| 1340 |
type2test = bytearray |
| 1341 |
|
| 1342 |
_testlimitedcapi = import_helper.import_module('_testlimitedcapi') |
| 1343 |
|
| 1344 |
def test_getitem_error(self): |
| 1345 |
b = bytearray(b'python') |
| 1346 |
msg = "bytearray indices must be integers or slices" |
| 1347 |
with self.assertRaisesRegex(TypeError, msg): |
| 1348 |
b['a'] |
| 1349 |
|
| 1350 |
def test_setitem_error(self): |
| 1351 |
b = bytearray(b'python') |
| 1352 |
msg = "bytearray indices must be integers or slices" |
| 1353 |
with self.assertRaisesRegex(TypeError, msg): |
| 1354 |
b['a'] = "python" |
| 1355 |
|
| 1356 |
def test_nohash(self): |
| 1357 |
self.assertRaises(TypeError, hash, bytearray()) |
| 1358 |
|
| 1359 |
def test_bytearray_api(self): |
| 1360 |
short_sample = b"Hello world\n" |
| 1361 |
sample = short_sample + b"\0"*(20 - len(short_sample)) |
| 1362 |
tfn = tempfile.mktemp() |
| 1363 |
try: |
| 1364 |
# Prepare |
| 1365 |
with open(tfn, "wb") as f: |
| 1366 |
f.write(short_sample) |
| 1367 |
# Test readinto |
| 1368 |
with open(tfn, "rb") as f: |
| 1369 |
b = bytearray(20) |
| 1370 |
n = f.readinto(b) |
| 1371 |
self.assertEqual(n, len(short_sample)) |
| 1372 |
self.assertEqual(list(b), list(sample)) |
| 1373 |
# Test writing in binary mode |
| 1374 |
with open(tfn, "wb") as f: |
| 1375 |
f.write(b) |
| 1376 |
with open(tfn, "rb") as f: |
| 1377 |
self.assertEqual(f.read(), sample) |
| 1378 |
# Text mode is ambiguous; don't test |
| 1379 |
finally: |
| 1380 |
try: |
| 1381 |
os.remove(tfn) |
| 1382 |
except OSError: |
| 1383 |
pass |
| 1384 |
|
| 1385 |
def test_mod_concurrent_mutation(self): |
| 1386 |
# Prevent crash in __mod__ when formatting mutates the bytearray. |
| 1387 |
# Regression test for https://github.com/python/cpython/issues/142557. |
| 1388 |
fmt = bytearray(b"%a end") |
| 1389 |
|
| 1390 |
class S: |
| 1391 |
def __repr__(self): |
| 1392 |
fmt.clear() |
| 1393 |
return "E" |
| 1394 |
|
| 1395 |
self.assertRaises(BufferError, fmt.__mod__, S()) |
| 1396 |
|
| 1397 |
def test_reverse(self): |
| 1398 |
b = bytearray(b'hello') |
| 1399 |
self.assertEqual(b.reverse(), None) |
| 1400 |
self.assertEqual(b, b'olleh') |
| 1401 |
b = bytearray(b'hello1') # test even number of items |
| 1402 |
b.reverse() |
| 1403 |
self.assertEqual(b, b'1olleh') |
| 1404 |
b = bytearray() |
| 1405 |
b.reverse() |
| 1406 |
self.assertFalse(b) |
| 1407 |
|
| 1408 |
def test_clear(self): |
| 1409 |
b = bytearray(b'python') |
| 1410 |
b.clear() |
| 1411 |
self.assertEqual(b, b'') |
| 1412 |
|
| 1413 |
b = bytearray(b'') |
| 1414 |
b.clear() |
| 1415 |
self.assertEqual(b, b'') |
| 1416 |
|
| 1417 |
b = bytearray(b'') |
| 1418 |
b.append(ord('r')) |
| 1419 |
b.clear() |
| 1420 |
b.append(ord('p')) |
| 1421 |
self.assertEqual(b, b'p') |
| 1422 |
|
| 1423 |
def test_copy(self): |
| 1424 |
b = bytearray(b'abc') |
| 1425 |
bb = b.copy() |
| 1426 |
self.assertEqual(bb, b'abc') |
| 1427 |
|
| 1428 |
b = bytearray(b'') |
| 1429 |
bb = b.copy() |
| 1430 |
self.assertEqual(bb, b'') |
| 1431 |
|
| 1432 |
# test that it's indeed a copy and not a reference |
| 1433 |
b = bytearray(b'abc') |
| 1434 |
bb = b.copy() |
| 1435 |
self.assertEqual(b, bb) |
| 1436 |
self.assertIsNot(b, bb) |
| 1437 |
bb.append(ord('d')) |
| 1438 |
self.assertEqual(bb, b'abcd') |
| 1439 |
self.assertEqual(b, b'abc') |
| 1440 |
|
| 1441 |
def test_regexps(self): |
| 1442 |
def by(s): |
| 1443 |
return bytearray(map(ord, s)) |
| 1444 |
b = by("Hello, world") |
| 1445 |
self.assertEqual(re.findall(br"\w+", b), [by("Hello"), by("world")]) |
| 1446 |
|
| 1447 |
def test_resize(self): |
| 1448 |
ba = bytearray(b'abcdef') |
| 1449 |
self.assertIsNone(ba.resize(3)) |
| 1450 |
self.assertEqual(ba, bytearray(b'abc')) |
| 1451 |
|
| 1452 |
self.assertIsNone(ba.resize(10)) |
| 1453 |
self.assertEqual(len(ba), 10) |
| 1454 |
# Bytes beyond set values must be cleared. |
| 1455 |
self.assertEqual(ba, bytearray(b'abc\0\0\0\0\0\0\0')) |
| 1456 |
|
| 1457 |
ba[3:10] = b'defghij' |
| 1458 |
self.assertEqual(ba, bytearray(b'abcdefghij')) |
| 1459 |
|
| 1460 |
self.assertIsNone(ba.resize(2 ** 20)) |
| 1461 |
self.assertEqual(len(ba), 2**20) |
| 1462 |
self.assertEqual(ba, bytearray(b'abcdefghij' + b'\0' * (2 ** 20 - 10))) |
| 1463 |
|
| 1464 |
self.assertIsNone(ba.resize(0)) |
| 1465 |
self.assertEqual(ba, bytearray()) |
| 1466 |
|
| 1467 |
self.assertIsNone(ba.resize(10)) |
| 1468 |
self.assertEqual(ba, bytearray(b'\0' * 10)) |
| 1469 |
|
| 1470 |
# Subclass |
| 1471 |
ba = ByteArraySubclass(b'abcdef') |
| 1472 |
self.assertIsNone(ba.resize(3)) |
| 1473 |
self.assertEqual(ba, bytearray(b'abc')) |
| 1474 |
|
| 1475 |
# Check arguments |
| 1476 |
self.assertRaises(TypeError, bytearray().resize) |
| 1477 |
self.assertRaises(TypeError, bytearray().resize, (10, 10)) |
| 1478 |
|
| 1479 |
self.assertRaises(ValueError, bytearray().resize, -1) |
| 1480 |
self.assertRaises(ValueError, bytearray().resize, -200) |
| 1481 |
self.assertRaises(MemoryError, bytearray().resize, sys.maxsize) |
| 1482 |
self.assertRaises(MemoryError, bytearray(1000).resize, sys.maxsize) |
| 1483 |
|
| 1484 |
|
| 1485 |
def test_setitem(self): |
| 1486 |
def setitem_as_mapping(b, i, val): |
| 1487 |
b[i] = val |
| 1488 |
|
| 1489 |
def setitem_as_sequence(b, i, val): |
| 1490 |
self._testlimitedcapi.sequence_setitem(b, i, val) |
| 1491 |
|
| 1492 |
def do_tests(setitem): |
| 1493 |
b = bytearray([1, 2, 3]) |
| 1494 |
setitem(b, 1, 100) |
| 1495 |
self.assertEqual(b, bytearray([1, 100, 3])) |
| 1496 |
setitem(b, -1, 200) |
| 1497 |
self.assertEqual(b, bytearray([1, 100, 200])) |
| 1498 |
setitem(b, 0, Indexable(10)) |
| 1499 |
self.assertEqual(b, bytearray([10, 100, 200])) |
| 1500 |
try: |
| 1501 |
setitem(b, 3, 0) |
| 1502 |
self.fail("Didn't raise IndexError") |
| 1503 |
except IndexError: |
| 1504 |
pass |
| 1505 |
try: |
| 1506 |
setitem(b, -10, 0) |
| 1507 |
self.fail("Didn't raise IndexError") |
| 1508 |
except IndexError: |
| 1509 |
pass |
| 1510 |
try: |
| 1511 |
setitem(b, 0, 256) |
| 1512 |
self.fail("Didn't raise ValueError") |
| 1513 |
except ValueError: |
| 1514 |
pass |
| 1515 |
try: |
| 1516 |
setitem(b, 0, Indexable(-1)) |
| 1517 |
self.fail("Didn't raise ValueError") |
| 1518 |
except ValueError: |
| 1519 |
pass |
| 1520 |
try: |
| 1521 |
setitem(b, 0, object()) |
| 1522 |
self.fail("Didn't raise TypeError") |
| 1523 |
except TypeError: |
| 1524 |
pass |
| 1525 |
|
| 1526 |
with self.subTest("tp_as_mapping"): |
| 1527 |
do_tests(setitem_as_mapping) |
| 1528 |
|
| 1529 |
with self.subTest("tp_as_sequence"): |
| 1530 |
do_tests(setitem_as_sequence) |
| 1531 |
|
| 1532 |
def test_delitem(self): |
| 1533 |
def del_as_mapping(b, i): |
| 1534 |
del b[i] |
| 1535 |
|
| 1536 |
def del_as_sequence(b, i): |
| 1537 |
self._testlimitedcapi.sequence_delitem(b, i) |
| 1538 |
|
| 1539 |
def do_tests(delete): |
| 1540 |
b = bytearray(range(10)) |
| 1541 |
delete(b, 0) |
| 1542 |
self.assertEqual(b, bytearray(range(1, 10))) |
| 1543 |
delete(b, -1) |
| 1544 |
self.assertEqual(b, bytearray(range(1, 9))) |
| 1545 |
delete(b, 4) |
| 1546 |
self.assertEqual(b, bytearray([1, 2, 3, 4, 6, 7, 8])) |
| 1547 |
|
| 1548 |
with self.subTest("tp_as_mapping"): |
| 1549 |
do_tests(del_as_mapping) |
| 1550 |
|
| 1551 |
with self.subTest("tp_as_sequence"): |
| 1552 |
do_tests(del_as_sequence) |
| 1553 |
|
| 1554 |
def test_setslice(self): |
| 1555 |
b = bytearray(range(10)) |
| 1556 |
self.assertEqual(list(b), list(range(10))) |
| 1557 |
|
| 1558 |
b[0:5] = bytearray([1, 1, 1, 1, 1]) |
| 1559 |
self.assertEqual(b, bytearray([1, 1, 1, 1, 1, 5, 6, 7, 8, 9])) |
| 1560 |
|
| 1561 |
del b[0:-5] |
| 1562 |
self.assertEqual(b, bytearray([5, 6, 7, 8, 9])) |
| 1563 |
|
| 1564 |
b[0:0] = bytearray([0, 1, 2, 3, 4]) |
| 1565 |
self.assertEqual(b, bytearray(range(10))) |
| 1566 |
|
| 1567 |
b[-7:-3] = bytearray([100, 101]) |
| 1568 |
self.assertEqual(b, bytearray([0, 1, 2, 100, 101, 7, 8, 9])) |
| 1569 |
|
| 1570 |
b[3:5] = [3, 4, 5, 6] |
| 1571 |
self.assertEqual(b, bytearray(range(10))) |
| 1572 |
|
| 1573 |
b[3:0] = [42, 42, 42] |
| 1574 |
self.assertEqual(b, bytearray([0, 1, 2, 42, 42, 42, 3, 4, 5, 6, 7, 8, 9])) |
| 1575 |
|
| 1576 |
b[3:] = b'foo' |
| 1577 |
self.assertEqual(b, bytearray([0, 1, 2, 102, 111, 111])) |
| 1578 |
|
| 1579 |
b[:3] = memoryview(b'foo') |
| 1580 |
self.assertEqual(b, bytearray([102, 111, 111, 102, 111, 111])) |
| 1581 |
|
| 1582 |
b[3:4] = [] |
| 1583 |
self.assertEqual(b, bytearray([102, 111, 111, 111, 111])) |
| 1584 |
|
| 1585 |
for elem in [5, -5, 0, int(10e20), 'str', 2.3, |
| 1586 |
['a', 'b'], [b'a', b'b'], [[]]]: |
| 1587 |
with self.assertRaises(TypeError): |
| 1588 |
b[3:4] = elem |
| 1589 |
|
| 1590 |
for elem in [[254, 255, 256], [-256, 9000]]: |
| 1591 |
with self.assertRaises(ValueError): |
| 1592 |
b[3:4] = elem |
| 1593 |
|
| 1594 |
def test_setslice_extend(self): |
| 1595 |
# Exercise the resizing logic (see issue #19087) |
| 1596 |
b = bytearray(range(100)) |
| 1597 |
self.assertEqual(list(b), list(range(100))) |
| 1598 |
del b[:10] |
| 1599 |
self.assertEqual(list(b), list(range(10, 100))) |
| 1600 |
b.extend(range(100, 110)) |
| 1601 |
self.assertEqual(list(b), list(range(10, 110))) |
| 1602 |
|
| 1603 |
def test_fifo_overrun(self): |
| 1604 |
# Test for issue #23985, a buffer overrun when implementing a FIFO |
| 1605 |
# Build Python in pydebug mode for best results. |
| 1606 |
b = bytearray(10) |
| 1607 |
b.pop() # Defeat expanding buffer off-by-one quirk |
| 1608 |
del b[:1] # Advance start pointer without reallocating |
| 1609 |
b += bytes(2) # Append exactly the number of deleted bytes |
| 1610 |
del b # Free memory buffer, allowing pydebug verification |
| 1611 |
|
| 1612 |
def test_del_expand(self): |
| 1613 |
# Reducing the size should not expand the buffer (issue #23985) |
| 1614 |
b = bytearray(10) |
| 1615 |
size = sys.getsizeof(b) |
| 1616 |
del b[:1] |
| 1617 |
self.assertLessEqual(sys.getsizeof(b), size) |
| 1618 |
|
| 1619 |
def test_extended_set_del_slice(self): |
| 1620 |
indices = (0, None, 1, 3, 19, 300, 1<<333, sys.maxsize, |
| 1621 |
-1, -2, -31, -300) |
| 1622 |
for start in indices: |
| 1623 |
for stop in indices: |
| 1624 |
# Skip invalid step 0 |
| 1625 |
for step in indices[1:]: |
| 1626 |
L = list(range(255)) |
| 1627 |
b = bytearray(L) |
| 1628 |
# Make sure we have a slice of exactly the right length, |
| 1629 |
# but with different data. |
| 1630 |
data = L[start:stop:step] |
| 1631 |
data.reverse() |
| 1632 |
L[start:stop:step] = data |
| 1633 |
b[start:stop:step] = data |
| 1634 |
self.assertEqual(b, bytearray(L)) |
| 1635 |
|
| 1636 |
del L[start:stop:step] |
| 1637 |
del b[start:stop:step] |
| 1638 |
self.assertEqual(b, bytearray(L)) |
| 1639 |
|
| 1640 |
def test_setslice_trap(self): |
| 1641 |
# This test verifies that we correctly handle assigning self |
| 1642 |
# to a slice of self (the old Lambert Meertens trap). |
| 1643 |
b = bytearray(range(256)) |
| 1644 |
b[8:] = b |
| 1645 |
self.assertEqual(b, bytearray(list(range(8)) + list(range(256)))) |
| 1646 |
|
| 1647 |
def test_iconcat(self): |
| 1648 |
b = bytearray(b"abc") |
| 1649 |
b1 = b |
| 1650 |
b += b"def" |
| 1651 |
self.assertEqual(b, b"abcdef") |
| 1652 |
self.assertEqual(b, b1) |
| 1653 |
self.assertIs(b, b1) |
| 1654 |
b += b"xyz" |
| 1655 |
self.assertEqual(b, b"abcdefxyz") |
| 1656 |
try: |
| 1657 |
b += "" |
| 1658 |
except TypeError: |
| 1659 |
pass |
| 1660 |
else: |
| 1661 |
self.fail("bytes += unicode didn't raise TypeError") |
| 1662 |
|
| 1663 |
def test_irepeat(self): |
| 1664 |
b = bytearray(b"abc") |
| 1665 |
b1 = b |
| 1666 |
b *= 3 |
| 1667 |
self.assertEqual(b, b"abcabcabc") |
| 1668 |
self.assertEqual(b, b1) |
| 1669 |
self.assertIs(b, b1) |
| 1670 |
|
| 1671 |
def test_irepeat_1char(self): |
| 1672 |
b = bytearray(b"x") |
| 1673 |
b1 = b |
| 1674 |
b *= 100 |
| 1675 |
self.assertEqual(b, b"x"*100) |
| 1676 |
self.assertEqual(b, b1) |
| 1677 |
self.assertIs(b, b1) |
| 1678 |
|
| 1679 |
def test_alloc(self): |
| 1680 |
b = bytearray() |
| 1681 |
alloc = b.__alloc__() |
| 1682 |
self.assertGreaterEqual(alloc, 0) |
| 1683 |
seq = [alloc] |
| 1684 |
for i in range(100): |
| 1685 |
b += b"x" |
| 1686 |
alloc = b.__alloc__() |
| 1687 |
self.assertGreater(alloc, len(b)) # including trailing null byte |
| 1688 |
if alloc not in seq: |
| 1689 |
seq.append(alloc) |
| 1690 |
|
| 1691 |
def test_init_alloc(self): |
| 1692 |
b = bytearray() |
| 1693 |
def g(): |
| 1694 |
for i in range(1, 100): |
| 1695 |
yield i |
| 1696 |
a = list(b) |
| 1697 |
self.assertEqual(a, list(range(1, len(a)+1))) |
| 1698 |
self.assertEqual(len(b), len(a)) |
| 1699 |
self.assertLessEqual(len(b), i) |
| 1700 |
alloc = b.__alloc__() |
| 1701 |
self.assertGreater(alloc, len(b)) # including trailing null byte |
| 1702 |
b.__init__(g()) |
| 1703 |
self.assertEqual(list(b), list(range(1, 100))) |
| 1704 |
self.assertEqual(len(b), 99) |
| 1705 |
alloc = b.__alloc__() |
| 1706 |
self.assertGreater(alloc, len(b)) |
| 1707 |
|
| 1708 |
def test_extend(self): |
| 1709 |
orig = b'hello' |
| 1710 |
a = bytearray(orig) |
| 1711 |
a.extend(a) |
| 1712 |
self.assertEqual(a, orig + orig) |
| 1713 |
self.assertEqual(a[5:], orig) |
| 1714 |
a = bytearray(b'') |
| 1715 |
# Test iterators that don't have a __length_hint__ |
| 1716 |
a.extend(map(int, orig * 25)) |
| 1717 |
a.extend(int(x) for x in orig * 25) |
| 1718 |
self.assertEqual(a, orig * 50) |
| 1719 |
self.assertEqual(a[-5:], orig) |
| 1720 |
a = bytearray(b'') |
| 1721 |
a.extend(iter(map(int, orig * 50))) |
| 1722 |
self.assertEqual(a, orig * 50) |
| 1723 |
self.assertEqual(a[-5:], orig) |
| 1724 |
a = bytearray(b'') |
| 1725 |
a.extend(list(map(int, orig * 50))) |
| 1726 |
self.assertEqual(a, orig * 50) |
| 1727 |
self.assertEqual(a[-5:], orig) |
| 1728 |
a = bytearray(b'') |
| 1729 |
self.assertRaises(ValueError, a.extend, [0, 1, 2, 256]) |
| 1730 |
self.assertRaises(ValueError, a.extend, [0, 1, 2, -1]) |
| 1731 |
self.assertEqual(len(a), 0) |
| 1732 |
a = bytearray(b'') |
| 1733 |
a.extend([Indexable(ord('a'))]) |
| 1734 |
self.assertEqual(a, b'a') |
| 1735 |
a = bytearray(b'abc') |
| 1736 |
self.assertRaisesRegex(TypeError, # Override for string. |
| 1737 |
"expected iterable of integers; got: 'str'", |
| 1738 |
a.extend, 'def') |
| 1739 |
self.assertRaisesRegex(TypeError, # But not for others. |
| 1740 |
"can't extend bytearray with float", |
| 1741 |
a.extend, 1.0) |
| 1742 |
|
| 1743 |
def test_remove(self): |
| 1744 |
b = bytearray(b'hello') |
| 1745 |
b.remove(ord('l')) |
| 1746 |
self.assertEqual(b, b'helo') |
| 1747 |
b.remove(ord('l')) |
| 1748 |
self.assertEqual(b, b'heo') |
| 1749 |
self.assertRaises(ValueError, lambda: b.remove(ord('l'))) |
| 1750 |
self.assertRaises(ValueError, lambda: b.remove(400)) |
| 1751 |
self.assertRaises(TypeError, lambda: b.remove('e')) |
| 1752 |
# remove first and last |
| 1753 |
b.remove(ord('o')) |
| 1754 |
b.remove(ord('h')) |
| 1755 |
self.assertEqual(b, b'e') |
| 1756 |
self.assertRaises(TypeError, lambda: b.remove(b'e')) |
| 1757 |
b.remove(Indexable(ord('e'))) |
| 1758 |
self.assertEqual(b, b'') |
| 1759 |
|
| 1760 |
# test values outside of the ascii range: (0, 127) |
| 1761 |
c = bytearray([126, 127, 128, 129]) |
| 1762 |
c.remove(127) |
| 1763 |
self.assertEqual(c, bytes([126, 128, 129])) |
| 1764 |
c.remove(129) |
| 1765 |
self.assertEqual(c, bytes([126, 128])) |
| 1766 |
|
| 1767 |
def test_pop(self): |
| 1768 |
b = bytearray(b'world') |
| 1769 |
self.assertEqual(b.pop(), ord('d')) |
| 1770 |
self.assertEqual(b.pop(0), ord('w')) |
| 1771 |
self.assertEqual(b.pop(-2), ord('r')) |
| 1772 |
self.assertRaises(IndexError, lambda: b.pop(10)) |
| 1773 |
self.assertRaises(IndexError, lambda: bytearray().pop()) |
| 1774 |
# test for issue #6846 |
| 1775 |
self.assertEqual(bytearray(b'\xff').pop(), 0xff) |
| 1776 |
|
| 1777 |
def test_nosort(self): |
| 1778 |
self.assertRaises(AttributeError, lambda: bytearray().sort()) |
| 1779 |
|
| 1780 |
def test_append(self): |
| 1781 |
b = bytearray(b'hell') |
| 1782 |
b.append(ord('o')) |
| 1783 |
self.assertEqual(b, b'hello') |
| 1784 |
self.assertEqual(b.append(100), None) |
| 1785 |
b = bytearray() |
| 1786 |
b.append(ord('A')) |
| 1787 |
self.assertEqual(len(b), 1) |
| 1788 |
self.assertRaises(TypeError, lambda: b.append(b'o')) |
| 1789 |
b = bytearray() |
| 1790 |
b.append(Indexable(ord('A'))) |
| 1791 |
self.assertEqual(b, b'A') |
| 1792 |
|
| 1793 |
def test_insert(self): |
| 1794 |
b = bytearray(b'msssspp') |
| 1795 |
b.insert(1, ord('i')) |
| 1796 |
b.insert(4, ord('i')) |
| 1797 |
b.insert(-2, ord('i')) |
| 1798 |
b.insert(1000, ord('i')) |
| 1799 |
self.assertEqual(b, b'mississippi') |
| 1800 |
self.assertRaises(TypeError, lambda: b.insert(0, b'1')) |
| 1801 |
b = bytearray() |
| 1802 |
b.insert(0, Indexable(ord('A'))) |
| 1803 |
self.assertEqual(b, b'A') |
| 1804 |
|
| 1805 |
def test_copied(self): |
| 1806 |
# Issue 4348. Make sure that operations that don't mutate the array |
| 1807 |
# copy the bytes. |
| 1808 |
b = bytearray(b'abc') |
| 1809 |
self.assertIsNot(b, b.replace(b'abc', b'cde', 0)) |
| 1810 |
|
| 1811 |
t = bytearray([i for i in range(256)]) |
| 1812 |
x = bytearray(b'') |
| 1813 |
self.assertIsNot(x, x.translate(t)) |
| 1814 |
|
| 1815 |
def test_partition_bytearray_doesnt_share_nullstring(self): |
| 1816 |
a, b, c = bytearray(b"x").partition(b"y") |
| 1817 |
self.assertEqual(b, b"") |
| 1818 |
self.assertEqual(c, b"") |
| 1819 |
self.assertIsNot(b, c) |
| 1820 |
b += b"!" |
| 1821 |
self.assertEqual(c, b"") |
| 1822 |
a, b, c = bytearray(b"x").partition(b"y") |
| 1823 |
self.assertEqual(b, b"") |
| 1824 |
self.assertEqual(c, b"") |
| 1825 |
# Same for rpartition |
| 1826 |
b, c, a = bytearray(b"x").rpartition(b"y") |
| 1827 |
self.assertEqual(b, b"") |
| 1828 |
self.assertEqual(c, b"") |
| 1829 |
self.assertIsNot(b, c) |
| 1830 |
b += b"!" |
| 1831 |
self.assertEqual(c, b"") |
| 1832 |
c, b, a = bytearray(b"x").rpartition(b"y") |
| 1833 |
self.assertEqual(b, b"") |
| 1834 |
self.assertEqual(c, b"") |
| 1835 |
|
| 1836 |
def test_resize_forbidden(self): |
| 1837 |
# #4509: can't resize a bytearray when there are buffer exports, even |
| 1838 |
# if it wouldn't reallocate the underlying buffer. |
| 1839 |
# Furthermore, no destructive changes to the buffer may be applied |
| 1840 |
# before raising the error. |
| 1841 |
b = bytearray(10) |
| 1842 |
v = memoryview(b) |
| 1843 |
def manual_resize(n): |
| 1844 |
b[1:-1] = range(n + 1, 2*n - 1) |
| 1845 |
b.resize(10) |
| 1846 |
orig = b[:] |
| 1847 |
self.assertRaises(BufferError, b.resize, 11) |
| 1848 |
self.assertRaises(BufferError, manual_resize, 11) |
| 1849 |
self.assertEqual(b, orig) |
| 1850 |
self.assertRaises(BufferError, b.resize, 9) |
| 1851 |
self.assertEqual(b, orig) |
| 1852 |
self.assertRaises(BufferError, b.resize, 0) |
| 1853 |
self.assertEqual(b, orig) |
| 1854 |
# Other operations implying resize |
| 1855 |
self.assertRaises(BufferError, b.pop, 0) |
| 1856 |
self.assertEqual(b, orig) |
| 1857 |
self.assertRaises(BufferError, b.remove, b[1]) |
| 1858 |
self.assertEqual(b, orig) |
| 1859 |
def delitem(): |
| 1860 |
del b[1] |
| 1861 |
self.assertRaises(BufferError, delitem) |
| 1862 |
self.assertEqual(b, orig) |
| 1863 |
# deleting a non-contiguous slice |
| 1864 |
def delslice(): |
| 1865 |
b[1:-1:2] = b"" |
| 1866 |
self.assertRaises(BufferError, delslice) |
| 1867 |
self.assertEqual(b, orig) |
| 1868 |
|
| 1869 |
@test.support.cpython_only |
| 1870 |
def test_obsolete_write_lock(self): |
| 1871 |
_testcapi = import_helper.import_module('_testcapi') |
| 1872 |
self.assertRaises(BufferError, _testcapi.getbuffer_with_null_view, bytearray()) |
| 1873 |
|
| 1874 |
def test_iterator_pickling2(self): |
| 1875 |
orig = bytearray(b'abc') |
| 1876 |
data = list(b'qwerty') |
| 1877 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1878 |
# initial iterator |
| 1879 |
itorig = iter(orig) |
| 1880 |
d = pickle.dumps((itorig, orig), proto) |
| 1881 |
it, b = pickle.loads(d) |
| 1882 |
b[:] = data |
| 1883 |
self.assertEqual(type(it), type(itorig)) |
| 1884 |
self.assertEqual(list(it), data) |
| 1885 |
|
| 1886 |
# running iterator |
| 1887 |
next(itorig) |
| 1888 |
d = pickle.dumps((itorig, orig), proto) |
| 1889 |
it, b = pickle.loads(d) |
| 1890 |
b[:] = data |
| 1891 |
self.assertEqual(type(it), type(itorig)) |
| 1892 |
self.assertEqual(list(it), data[1:]) |
| 1893 |
|
| 1894 |
# empty iterator |
| 1895 |
for i in range(1, len(orig)): |
| 1896 |
next(itorig) |
| 1897 |
d = pickle.dumps((itorig, orig), proto) |
| 1898 |
it, b = pickle.loads(d) |
| 1899 |
b[:] = data |
| 1900 |
self.assertEqual(type(it), type(itorig)) |
| 1901 |
self.assertEqual(list(it), data[len(orig):]) |
| 1902 |
|
| 1903 |
# exhausted iterator |
| 1904 |
self.assertRaises(StopIteration, next, itorig) |
| 1905 |
d = pickle.dumps((itorig, orig), proto) |
| 1906 |
it, b = pickle.loads(d) |
| 1907 |
b[:] = data |
| 1908 |
self.assertEqual(list(it), []) |
| 1909 |
|
| 1910 |
test_exhausted_iterator = test.list_tests.CommonTest.test_exhausted_iterator |
| 1911 |
|
| 1912 |
def test_iterator_length_hint(self): |
| 1913 |
# Issue 27443: __length_hint__ can return negative integer |
| 1914 |
ba = bytearray(b'ab') |
| 1915 |
it = iter(ba) |
| 1916 |
next(it) |
| 1917 |
ba.clear() |
| 1918 |
# Shouldn't raise an error |
| 1919 |
self.assertEqual(list(it), []) |
| 1920 |
|
| 1921 |
def test_repeat_after_setslice(self): |
| 1922 |
# bpo-42924: * used to copy from the wrong memory location |
| 1923 |
b = bytearray(b'abc') |
| 1924 |
b[:2] = b'x' |
| 1925 |
b1 = b * 1 |
| 1926 |
b3 = b * 3 |
| 1927 |
self.assertEqual(b1, b'xc') |
| 1928 |
self.assertEqual(b1, b) |
| 1929 |
self.assertEqual(b3, b'xcxcxc') |
| 1930 |
|
| 1931 |
def test_mutating_index(self): |
| 1932 |
# bytearray slice assignment can call into python code |
| 1933 |
# that reallocates the internal buffer |
| 1934 |
# See gh-91153 |
| 1935 |
|
| 1936 |
class Boom: |
| 1937 |
def __index__(self): |
| 1938 |
b.clear() |
| 1939 |
return 0 |
| 1940 |
|
| 1941 |
with self.subTest("tp_as_mapping"): |
| 1942 |
b = bytearray(b'Now you see me...') |
| 1943 |
with self.assertRaises(IndexError): |
| 1944 |
b[0] = Boom() |
| 1945 |
|
| 1946 |
with self.subTest("tp_as_sequence"): |
| 1947 |
b = bytearray(b'Now you see me...') |
| 1948 |
with self.assertRaises(IndexError): |
| 1949 |
self._testlimitedcapi.sequence_setitem(b, 0, Boom()) |
| 1950 |
|
| 1951 |
def test_mutating_index_inbounds(self): |
| 1952 |
# gh-91153 continued |
| 1953 |
# Ensure buffer is not broken even if length is correct |
| 1954 |
|
| 1955 |
class MutatesOnIndex: |
| 1956 |
def __init__(self): |
| 1957 |
self.ba = bytearray(0x180) |
| 1958 |
|
| 1959 |
def __index__(self): |
| 1960 |
self.ba.clear() |
| 1961 |
self.new_ba = bytearray(0x180) # to catch out-of-bounds writes |
| 1962 |
self.ba.extend([0] * 0x180) # to check bounds checks |
| 1963 |
return 0 |
| 1964 |
|
| 1965 |
with self.subTest("skip_bounds_safety"): |
| 1966 |
instance = MutatesOnIndex() |
| 1967 |
instance.ba[instance] = ord("?") |
| 1968 |
self.assertEqual(instance.ba[0], ord("?"), "Assigned bytearray not altered") |
| 1969 |
self.assertEqual(instance.new_ba, bytearray(0x180), "Wrong object altered") |
| 1970 |
|
| 1971 |
with self.subTest("skip_bounds_safety_capi"): |
| 1972 |
instance = MutatesOnIndex() |
| 1973 |
instance.ba[instance] = ord("?") |
| 1974 |
self._testlimitedcapi.sequence_setitem(instance.ba, instance, ord("?")) |
| 1975 |
self.assertEqual(instance.ba[0], ord("?"), "Assigned bytearray not altered") |
| 1976 |
self.assertEqual(instance.new_ba, bytearray(0x180), "Wrong object altered") |
| 1977 |
|
| 1978 |
with self.subTest("skip_bounds_safety_slice"): |
| 1979 |
instance = MutatesOnIndex() |
| 1980 |
instance.ba[instance:1] = [ord("?")] |
| 1981 |
self.assertEqual(instance.ba[0], ord("?"), "Assigned bytearray not altered") |
| 1982 |
self.assertEqual(instance.new_ba, bytearray(0x180), "Wrong object altered") |
| 1983 |
|
| 1984 |
def test_search_methods_reentrancy_raises_buffererror(self): |
| 1985 |
# gh-142560: Raise BufferError if buffer mutates during search arg conversion. |
| 1986 |
class Evil: |
| 1987 |
def __init__(self, ba): |
| 1988 |
self.ba = ba |
| 1989 |
def __buffer__(self, flags): |
| 1990 |
self.ba.clear() |
| 1991 |
return memoryview(self.ba) |
| 1992 |
def __release_buffer__(self, view: memoryview) -> None: |
| 1993 |
view.release() |
| 1994 |
def __index__(self): |
| 1995 |
self.ba.clear() |
| 1996 |
return ord("A") |
| 1997 |
|
| 1998 |
def make_case(): |
| 1999 |
ba = bytearray(b"A") |
| 2000 |
return ba, Evil(ba) |
| 2001 |
|
| 2002 |
for name in ("find", "count", "index", "rindex", "rfind"): |
| 2003 |
ba, evil = make_case() |
| 2004 |
with self.subTest(name): |
| 2005 |
with self.assertRaises(BufferError): |
| 2006 |
getattr(ba, name)(evil) |
| 2007 |
|
| 2008 |
ba, evil = make_case() |
| 2009 |
with self.assertRaises(BufferError): |
| 2010 |
evil in ba |
| 2011 |
with self.assertRaises(BufferError): |
| 2012 |
ba.split(evil) |
| 2013 |
with self.assertRaises(BufferError): |
| 2014 |
ba.rsplit(evil) |
| 2015 |
|
| 2016 |
def test_extend_empty_buffer_overflow(self): |
| 2017 |
# gh-143003 |
| 2018 |
class EvilIter: |
| 2019 |
def __iter__(self): |
| 2020 |
return self |
| 2021 |
def __next__(self): |
| 2022 |
return next(source) |
| 2023 |
def __length_hint__(self): |
| 2024 |
return 0 |
| 2025 |
|
| 2026 |
# Use ASCII digits so float() takes the fast path that expects a NUL terminator. |
| 2027 |
source = iter(b'42') |
| 2028 |
ba = bytearray() |
| 2029 |
ba.extend(EvilIter()) |
| 2030 |
|
| 2031 |
self.assertRaises(ValueError, float, bytearray()) |
| 2032 |
|
| 2033 |
def test_hex_use_after_free(self): |
| 2034 |
# Prevent UAF in bytearray.hex(sep) with re-entrant sep.__len__. |
| 2035 |
# Regression test for https://github.com/python/cpython/issues/143195. |
| 2036 |
ba = bytearray(b'\xAA') |
| 2037 |
|
| 2038 |
class S(bytes): |
| 2039 |
def __len__(self): |
| 2040 |
ba.clear() |
| 2041 |
return 1 |
| 2042 |
|
| 2043 |
self.assertRaises(BufferError, ba.hex, S(b':')) |
| 2044 |
|
| 2045 |
|
| 2046 |
class AssortedBytesTest(unittest.TestCase): |
| 2047 |
# |
| 2048 |
# Test various combinations of bytes and bytearray |
| 2049 |
# |
| 2050 |
|
| 2051 |
def test_bytes_repr(self, f=repr): |
| 2052 |
self.assertEqual(f(b''), "b''") |
| 2053 |
self.assertEqual(f(b"abc"), "b'abc'") |
| 2054 |
self.assertEqual(f(bytes([92])), r"b'\\'") |
| 2055 |
self.assertEqual(f(bytes([0, 1, 254, 255])), r"b'\x00\x01\xfe\xff'") |
| 2056 |
self.assertEqual(f(b'\a\b\t\n\v\f\r'), r"b'\x07\x08\t\n\x0b\x0c\r'") |
| 2057 |
self.assertEqual(f(b'"'), """b'"'""") # '"' |
| 2058 |
self.assertEqual(f(b"'"), '''b"'"''') # "'" |
| 2059 |
self.assertEqual(f(b"'\""), r"""b'\'"'""") # '\'"' |
| 2060 |
self.assertEqual(f(b"\"'\""), r"""b'"\'"'""") # '"\'"' |
| 2061 |
self.assertEqual(f(b"'\"'"), r"""b'\'"\''""") # '\'"\'' |
| 2062 |
self.assertEqual(f(BytesSubclass(b"abc")), "b'abc'") |
| 2063 |
|
| 2064 |
def test_bytearray_repr(self, f=repr): |
| 2065 |
self.assertEqual(f(bytearray()), "bytearray(b'')") |
| 2066 |
self.assertEqual(f(bytearray(b'abc')), "bytearray(b'abc')") |
| 2067 |
self.assertEqual(f(bytearray([92])), r"bytearray(b'\\')") |
| 2068 |
self.assertEqual(f(bytearray([0, 1, 254, 255])), |
| 2069 |
r"bytearray(b'\x00\x01\xfe\xff')") |
| 2070 |
self.assertEqual(f(bytearray([7, 8, 9, 10, 11, 12, 13])), |
| 2071 |
r"bytearray(b'\x07\x08\t\n\x0b\x0c\r')") |
| 2072 |
self.assertEqual(f(bytearray(b'"')), """bytearray(b'"')""") # '"' |
| 2073 |
self.assertEqual(f(bytearray(b"'")), r'''bytearray(b"\'")''') # "\'" |
| 2074 |
self.assertEqual(f(bytearray(b"'\"")), r"""bytearray(b'\'"')""") # '\'"' |
| 2075 |
self.assertEqual(f(bytearray(b"\"'\"")), r"""bytearray(b'"\'"')""") # '"\'"' |
| 2076 |
self.assertEqual(f(bytearray(b'\'"\'')), r"""bytearray(b'\'"\'')""") # '\'"\'' |
| 2077 |
self.assertEqual(f(ByteArraySubclass(b"abc")), "ByteArraySubclass(b'abc')") |
| 2078 |
self.assertEqual(f(ByteArraySubclass.Nested(b"abc")), "Nested(b'abc')") |
| 2079 |
self.assertEqual(f(ByteArraySubclass.Ŭñıçöđë(b"abc")), "Ŭñıçöđë(b'abc')") |
| 2080 |
|
| 2081 |
@check_bytes_warnings |
| 2082 |
def test_bytes_str(self): |
| 2083 |
self.test_bytes_repr(str) |
| 2084 |
|
| 2085 |
@check_bytes_warnings |
| 2086 |
def test_bytearray_str(self): |
| 2087 |
self.test_bytearray_repr(str) |
| 2088 |
|
| 2089 |
@check_bytes_warnings |
| 2090 |
def test_format(self): |
| 2091 |
for b in b'abc', bytearray(b'abc'): |
| 2092 |
self.assertEqual(format(b), str(b)) |
| 2093 |
self.assertEqual(format(b, ''), str(b)) |
| 2094 |
with self.assertRaisesRegex(TypeError, |
| 2095 |
r'\b%s\b' % re.escape(type(b).__name__)): |
| 2096 |
format(b, 's') |
| 2097 |
|
| 2098 |
def test_compare_bytes_to_bytearray(self): |
| 2099 |
self.assertEqual(b"abc" == bytes(b"abc"), True) |
| 2100 |
self.assertEqual(b"ab" != bytes(b"abc"), True) |
| 2101 |
self.assertEqual(b"ab" <= bytes(b"abc"), True) |
| 2102 |
self.assertEqual(b"ab" < bytes(b"abc"), True) |
| 2103 |
self.assertEqual(b"abc" >= bytes(b"ab"), True) |
| 2104 |
self.assertEqual(b"abc" > bytes(b"ab"), True) |
| 2105 |
|
| 2106 |
self.assertEqual(b"abc" != bytes(b"abc"), False) |
| 2107 |
self.assertEqual(b"ab" == bytes(b"abc"), False) |
| 2108 |
self.assertEqual(b"ab" > bytes(b"abc"), False) |
| 2109 |
self.assertEqual(b"ab" >= bytes(b"abc"), False) |
| 2110 |
self.assertEqual(b"abc" < bytes(b"ab"), False) |
| 2111 |
self.assertEqual(b"abc" <= bytes(b"ab"), False) |
| 2112 |
|
| 2113 |
self.assertEqual(bytes(b"abc") == b"abc", True) |
| 2114 |
self.assertEqual(bytes(b"ab") != b"abc", True) |
| 2115 |
self.assertEqual(bytes(b"ab") <= b"abc", True) |
| 2116 |
self.assertEqual(bytes(b"ab") < b"abc", True) |
| 2117 |
self.assertEqual(bytes(b"abc") >= b"ab", True) |
| 2118 |
self.assertEqual(bytes(b"abc") > b"ab", True) |
| 2119 |
|
| 2120 |
self.assertEqual(bytes(b"abc") != b"abc", False) |
| 2121 |
self.assertEqual(bytes(b"ab") == b"abc", False) |
| 2122 |
self.assertEqual(bytes(b"ab") > b"abc", False) |
| 2123 |
self.assertEqual(bytes(b"ab") >= b"abc", False) |
| 2124 |
self.assertEqual(bytes(b"abc") < b"ab", False) |
| 2125 |
self.assertEqual(bytes(b"abc") <= b"ab", False) |
| 2126 |
|
| 2127 |
@test.support.requires_docstrings |
| 2128 |
def test_doc(self): |
| 2129 |
self.assertIsNotNone(bytearray.__doc__) |
| 2130 |
self.assertStartsWith(bytearray.__doc__, "bytearray(") |
| 2131 |
self.assertIsNotNone(bytes.__doc__) |
| 2132 |
self.assertStartsWith(bytes.__doc__, "bytes(") |
| 2133 |
|
| 2134 |
def test_from_bytearray(self): |
| 2135 |
sample = bytes(b"Hello world\n\x80\x81\xfe\xff") |
| 2136 |
buf = memoryview(sample) |
| 2137 |
b = bytearray(buf) |
| 2138 |
self.assertEqual(b, bytearray(sample)) |
| 2139 |
|
| 2140 |
def test_literal(self): |
| 2141 |
tests = [ |
| 2142 |
(b"Wonderful spam", "Wonderful spam"), |
| 2143 |
(br"Wonderful spam too", "Wonderful spam too"), |
| 2144 |
(b"\xaa\x00\000\200", "\xaa\x00\000\200"), |
| 2145 |
(br"\xaa\x00\000\200", r"\xaa\x00\000\200"), |
| 2146 |
] |
| 2147 |
for b, s in tests: |
| 2148 |
self.assertEqual(b, bytearray(s, 'latin-1')) |
| 2149 |
for c in range(128, 256): |
| 2150 |
self.assertRaises(SyntaxError, eval, |
| 2151 |
'b"%s"' % chr(c)) |
| 2152 |
|
| 2153 |
def test_split_bytearray(self): |
| 2154 |
self.assertEqual(b'a b'.split(memoryview(b' ')), [b'a', b'b']) |
| 2155 |
|
| 2156 |
def test_rsplit_bytearray(self): |
| 2157 |
self.assertEqual(b'a b'.rsplit(memoryview(b' ')), [b'a', b'b']) |
| 2158 |
|
| 2159 |
def test_return_self(self): |
| 2160 |
# bytearray.replace must always return a new bytearray |
| 2161 |
b = bytearray() |
| 2162 |
self.assertIsNot(b.replace(b'', b''), b) |
| 2163 |
|
| 2164 |
@unittest.skipUnless(sys.flags.bytes_warning, |
| 2165 |
"BytesWarning is needed for this test: use -bb option") |
| 2166 |
def test_compare(self): |
| 2167 |
def bytes_warning(): |
| 2168 |
return warnings_helper.check_warnings(('', BytesWarning)) |
| 2169 |
with bytes_warning(): |
| 2170 |
b'' == '' |
| 2171 |
with bytes_warning(): |
| 2172 |
'' == b'' |
| 2173 |
with bytes_warning(): |
| 2174 |
b'' != '' |
| 2175 |
with bytes_warning(): |
| 2176 |
'' != b'' |
| 2177 |
with bytes_warning(): |
| 2178 |
bytearray(b'') == '' |
| 2179 |
with bytes_warning(): |
| 2180 |
'' == bytearray(b'') |
| 2181 |
with bytes_warning(): |
| 2182 |
bytearray(b'') != '' |
| 2183 |
with bytes_warning(): |
| 2184 |
'' != bytearray(b'') |
| 2185 |
with bytes_warning(): |
| 2186 |
b'\0' == 0 |
| 2187 |
with bytes_warning(): |
| 2188 |
0 == b'\0' |
| 2189 |
with bytes_warning(): |
| 2190 |
b'\0' != 0 |
| 2191 |
with bytes_warning(): |
| 2192 |
0 != b'\0' |
| 2193 |
|
| 2194 |
# Optimizations: |
| 2195 |
# __iter__? (optimization) |
| 2196 |
# __reversed__? (optimization) |
| 2197 |
|
| 2198 |
# XXX More string methods? (Those that don't use character properties) |
| 2199 |
|
| 2200 |
# There are tests in string_tests.py that are more |
| 2201 |
# comprehensive for things like partition, etc. |
| 2202 |
# Unfortunately they are all bundled with tests that |
| 2203 |
# are not appropriate for bytes |
| 2204 |
|
| 2205 |
# I've started porting some of those into bytearray_tests.py, we should port |
| 2206 |
# the rest that make sense (the code can be cleaned up to use modern |
| 2207 |
# unittest methods at the same time). |
| 2208 |
|
| 2209 |
class BytearrayPEP3137Test(unittest.TestCase): |
| 2210 |
def marshal(self, x): |
| 2211 |
return bytearray(x) |
| 2212 |
|
| 2213 |
def test_returns_new_copy(self): |
| 2214 |
val = self.marshal(b'1234') |
| 2215 |
# On immutable types these MAY return a reference to themselves |
| 2216 |
# but on mutable types like bytearray they MUST return a new copy. |
| 2217 |
for methname in ('zfill', 'rjust', 'ljust', 'center'): |
| 2218 |
method = getattr(val, methname) |
| 2219 |
newval = method(3) |
| 2220 |
self.assertEqual(val, newval) |
| 2221 |
self.assertIsNot(val, newval, |
| 2222 |
methname+' returned self on a mutable object') |
| 2223 |
for expr in ('val.split()[0]', 'val.rsplit()[0]', |
| 2224 |
'val.partition(b".")[0]', 'val.rpartition(b".")[2]', |
| 2225 |
'val.splitlines()[0]', 'val.replace(b"", b"")'): |
| 2226 |
newval = eval(expr) |
| 2227 |
self.assertEqual(val, newval) |
| 2228 |
self.assertIsNot(val, newval, |
| 2229 |
expr+' returned val on a mutable object') |
| 2230 |
sep = self.marshal(b'') |
| 2231 |
newval = sep.join([val]) |
| 2232 |
self.assertEqual(val, newval) |
| 2233 |
self.assertIsNot(val, newval) |
| 2234 |
|
| 2235 |
|
| 2236 |
class FixedStringTest(test.string_tests.BaseTest): |
| 2237 |
def fixtype(self, obj): |
| 2238 |
if isinstance(obj, str): |
| 2239 |
return self.type2test(obj.encode("utf-8")) |
| 2240 |
return super().fixtype(obj) |
| 2241 |
|
| 2242 |
contains_bytes = True |
| 2243 |
|
| 2244 |
def test_mixed_cmp(self): |
| 2245 |
a = self.type2test(b'ab') |
| 2246 |
for t in bytes, bytearray, BytesSubclass, ByteArraySubclass: |
| 2247 |
with self.subTest(t.__name__): |
| 2248 |
self._assert_cmp(a, t(b'ab'), 0) |
| 2249 |
self._assert_cmp(a, t(b'a'), 1) |
| 2250 |
self._assert_cmp(a, t(b'ac'), -1) |
| 2251 |
|
| 2252 |
class ByteArrayAsStringTest(FixedStringTest, unittest.TestCase): |
| 2253 |
type2test = bytearray |
| 2254 |
|
| 2255 |
class BytesAsStringTest(FixedStringTest, unittest.TestCase): |
| 2256 |
type2test = bytes |
| 2257 |
|
| 2258 |
class SubclassTest: |
| 2259 |
|
| 2260 |
def test_basic(self): |
| 2261 |
self.assertIsSubclass(self.type2test, self.basetype) |
| 2262 |
self.assertIsInstance(self.type2test(), self.basetype) |
| 2263 |
|
| 2264 |
a, b = b"abcd", b"efgh" |
| 2265 |
_a, _b = self.type2test(a), self.type2test(b) |
| 2266 |
|
| 2267 |
# test comparison operators with subclass instances |
| 2268 |
self.assertTrue(_a == _a) |
| 2269 |
self.assertTrue(_a != _b) |
| 2270 |
self.assertTrue(_a < _b) |
| 2271 |
self.assertTrue(_a <= _b) |
| 2272 |
self.assertTrue(_b >= _a) |
| 2273 |
self.assertTrue(_b > _a) |
| 2274 |
self.assertIsNot(_a, a) |
| 2275 |
|
| 2276 |
# test concat of subclass instances |
| 2277 |
self.assertEqual(a + b, _a + _b) |
| 2278 |
self.assertEqual(a + b, a + _b) |
| 2279 |
self.assertEqual(a + b, _a + b) |
| 2280 |
|
| 2281 |
# test repeat |
| 2282 |
self.assertTrue(a*5 == _a*5) |
| 2283 |
|
| 2284 |
def test_join(self): |
| 2285 |
# Make sure join returns a NEW object for single item sequences |
| 2286 |
# involving a subclass. |
| 2287 |
# Make sure that it is of the appropriate type. |
| 2288 |
s1 = self.type2test(b"abcd") |
| 2289 |
s2 = self.basetype().join([s1]) |
| 2290 |
self.assertIsNot(s1, s2) |
| 2291 |
self.assertIs(type(s2), self.basetype, type(s2)) |
| 2292 |
|
| 2293 |
# Test reverse, calling join on subclass |
| 2294 |
s3 = s1.join([b"abcd"]) |
| 2295 |
self.assertIs(type(s3), self.basetype) |
| 2296 |
|
| 2297 |
def test_pickle(self): |
| 2298 |
a = self.type2test(b"abcd") |
| 2299 |
a.x = 10 |
| 2300 |
a.z = self.type2test(b"efgh") |
| 2301 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 2302 |
b = pickle.loads(pickle.dumps(a, proto)) |
| 2303 |
self.assertNotEqual(id(a), id(b)) |
| 2304 |
self.assertEqual(a, b) |
| 2305 |
self.assertEqual(a.x, b.x) |
| 2306 |
self.assertEqual(a.z, b.z) |
| 2307 |
self.assertEqual(type(a), type(b)) |
| 2308 |
self.assertEqual(type(a.z), type(b.z)) |
| 2309 |
self.assertNotHasAttr(b, 'y') |
| 2310 |
|
| 2311 |
def test_copy(self): |
| 2312 |
a = self.type2test(b"abcd") |
| 2313 |
a.x = 10 |
| 2314 |
a.z = self.type2test(b"efgh") |
| 2315 |
for copy_method in (copy.copy, copy.deepcopy): |
| 2316 |
b = copy_method(a) |
| 2317 |
self.assertNotEqual(id(a), id(b)) |
| 2318 |
self.assertEqual(a, b) |
| 2319 |
self.assertEqual(a.x, b.x) |
| 2320 |
self.assertEqual(a.z, b.z) |
| 2321 |
self.assertEqual(type(a), type(b)) |
| 2322 |
self.assertEqual(type(a.z), type(b.z)) |
| 2323 |
self.assertNotHasAttr(b, 'y') |
| 2324 |
|
| 2325 |
def test_fromhex(self): |
| 2326 |
b = self.type2test.fromhex('1a2B30') |
| 2327 |
self.assertEqual(b, b'\x1a\x2b\x30') |
| 2328 |
self.assertIs(type(b), self.type2test) |
| 2329 |
|
| 2330 |
class B1(self.basetype): |
| 2331 |
def __new__(cls, value): |
| 2332 |
me = self.basetype.__new__(cls, value) |
| 2333 |
me.foo = 'bar' |
| 2334 |
return me |
| 2335 |
|
| 2336 |
b = B1.fromhex('1a2B30') |
| 2337 |
self.assertEqual(b, b'\x1a\x2b\x30') |
| 2338 |
self.assertIs(type(b), B1) |
| 2339 |
self.assertEqual(b.foo, 'bar') |
| 2340 |
|
| 2341 |
class B2(self.basetype): |
| 2342 |
def __init__(me, *args, **kwargs): |
| 2343 |
if self.basetype is not bytes: |
| 2344 |
self.basetype.__init__(me, *args, **kwargs) |
| 2345 |
me.foo = 'bar' |
| 2346 |
|
| 2347 |
b = B2.fromhex('1a2B30') |
| 2348 |
self.assertEqual(b, b'\x1a\x2b\x30') |
| 2349 |
self.assertIs(type(b), B2) |
| 2350 |
self.assertEqual(b.foo, 'bar') |
| 2351 |
|
| 2352 |
|
| 2353 |
class ByteArraySubclass(bytearray): |
| 2354 |
class Nested(bytearray): |
| 2355 |
pass |
| 2356 |
class Ŭñıçöđë(bytearray): |
| 2357 |
pass |
| 2358 |
|
| 2359 |
class ByteArraySubclassWithSlots(bytearray): |
| 2360 |
__slots__ = ('x', 'y', '__dict__') |
| 2361 |
|
| 2362 |
class BytesSubclass(bytes): |
| 2363 |
pass |
| 2364 |
|
| 2365 |
class OtherBytesSubclass(bytes): |
| 2366 |
pass |
| 2367 |
|
| 2368 |
class WithBytes: |
| 2369 |
def __init__(self, value): |
| 2370 |
self.value = value |
| 2371 |
def __bytes__(self): |
| 2372 |
return self.value |
| 2373 |
|
| 2374 |
class ByteArraySubclassTest(SubclassTest, unittest.TestCase): |
| 2375 |
basetype = bytearray |
| 2376 |
type2test = ByteArraySubclass |
| 2377 |
|
| 2378 |
def test_init_override(self): |
| 2379 |
class subclass(bytearray): |
| 2380 |
def __init__(me, newarg=1, *args, **kwargs): |
| 2381 |
bytearray.__init__(me, *args, **kwargs) |
| 2382 |
x = subclass(4, b"abcd") |
| 2383 |
x = subclass(4, source=b"abcd") |
| 2384 |
self.assertEqual(x, b"abcd") |
| 2385 |
x = subclass(newarg=4, source=b"abcd") |
| 2386 |
self.assertEqual(x, b"abcd") |
| 2387 |
|
| 2388 |
class ByteArraySubclassWithSlotsTest(SubclassTest, unittest.TestCase): |
| 2389 |
basetype = bytearray |
| 2390 |
type2test = ByteArraySubclassWithSlots |
| 2391 |
|
| 2392 |
class BytesSubclassTest(SubclassTest, unittest.TestCase): |
| 2393 |
basetype = bytes |
| 2394 |
type2test = BytesSubclass |
| 2395 |
|
| 2396 |
|
| 2397 |
class FreeThreadingTest(unittest.TestCase): |
| 2398 |
@unittest.skipUnless(support.Py_GIL_DISABLED, 'this test can only possibly fail with GIL disabled') |
| 2399 |
@threading_helper.reap_threads |
| 2400 |
@threading_helper.requires_working_threading() |
| 2401 |
def test_free_threading_bytearray(self): |
| 2402 |
# Test pretty much everything that can break under free-threading. |
| 2403 |
# Non-deterministic, but at least one of these things will fail if |
| 2404 |
# bytearray module is not free-thread safe. |
| 2405 |
|
| 2406 |
def clear(b, a, *args): # MODIFIES! |
| 2407 |
b.wait() |
| 2408 |
try: a.clear() |
| 2409 |
except BufferError: pass |
| 2410 |
|
| 2411 |
def clear2(b, a, c): # MODIFIES c! |
| 2412 |
b.wait() |
| 2413 |
try: c.clear() |
| 2414 |
except BufferError: pass |
| 2415 |
|
| 2416 |
def pop1(b, a): # MODIFIES! |
| 2417 |
b.wait() |
| 2418 |
try: a.pop() |
| 2419 |
except IndexError: pass |
| 2420 |
|
| 2421 |
def append1(b, a): # MODIFIES! |
| 2422 |
b.wait() |
| 2423 |
a.append(0) |
| 2424 |
|
| 2425 |
def insert1(b, a): # MODIFIES! |
| 2426 |
b.wait() |
| 2427 |
a.insert(0, 0) |
| 2428 |
|
| 2429 |
def extend(b, a): # MODIFIES! |
| 2430 |
c = bytearray(b'0' * 0x400000) |
| 2431 |
b.wait() |
| 2432 |
a.extend(c) |
| 2433 |
|
| 2434 |
def remove(b, a): # MODIFIES! |
| 2435 |
c = ord('0') |
| 2436 |
b.wait() |
| 2437 |
try: a.remove(c) |
| 2438 |
except ValueError: pass |
| 2439 |
|
| 2440 |
def reverse(b, a): # modifies inplace |
| 2441 |
b.wait() |
| 2442 |
a.reverse() |
| 2443 |
|
| 2444 |
def reduce(b, a): |
| 2445 |
b.wait() |
| 2446 |
a.__reduce__() |
| 2447 |
|
| 2448 |
def reduceex2(b, a): |
| 2449 |
b.wait() |
| 2450 |
a.__reduce_ex__(2) |
| 2451 |
|
| 2452 |
def reduceex3(b, a): |
| 2453 |
b.wait() |
| 2454 |
c = a.__reduce_ex__(3) |
| 2455 |
assert not c[1] or 0xdd not in c[1][0] |
| 2456 |
|
| 2457 |
def count0(b, a): |
| 2458 |
b.wait() |
| 2459 |
a.count(0) |
| 2460 |
|
| 2461 |
def decode(b, a): |
| 2462 |
b.wait() |
| 2463 |
a.decode() |
| 2464 |
|
| 2465 |
def find(b, a): |
| 2466 |
c = bytearray(b'0' * 0x40000) |
| 2467 |
b.wait() |
| 2468 |
a.find(c) |
| 2469 |
|
| 2470 |
def hex(b, a): |
| 2471 |
b.wait() |
| 2472 |
a.hex('_') |
| 2473 |
|
| 2474 |
def join(b, a): |
| 2475 |
b.wait() |
| 2476 |
a.join([b'1', b'2', b'3']) |
| 2477 |
|
| 2478 |
def replace(b, a): |
| 2479 |
b.wait() |
| 2480 |
a.replace(b'0', b'') |
| 2481 |
|
| 2482 |
def maketrans(b, a, c): |
| 2483 |
b.wait() |
| 2484 |
try: a.maketrans(a, c) |
| 2485 |
except ValueError: pass |
| 2486 |
|
| 2487 |
def translate(b, a, c): |
| 2488 |
b.wait() |
| 2489 |
a.translate(c) |
| 2490 |
|
| 2491 |
def copy(b, a): |
| 2492 |
b.wait() |
| 2493 |
c = a.copy() |
| 2494 |
if c: assert c[0] == 48 # '0' |
| 2495 |
|
| 2496 |
def endswith(b, a): |
| 2497 |
b.wait() |
| 2498 |
assert not a.endswith(b'\xdd') |
| 2499 |
|
| 2500 |
def index(b, a): |
| 2501 |
b.wait() |
| 2502 |
try: a.index(b'\xdd') |
| 2503 |
except ValueError: return |
| 2504 |
assert False |
| 2505 |
|
| 2506 |
def lstrip(b, a): |
| 2507 |
b.wait() |
| 2508 |
assert not a.lstrip(b'0') |
| 2509 |
|
| 2510 |
def partition(b, a): |
| 2511 |
b.wait() |
| 2512 |
assert not a.partition(b'\xdd')[2] |
| 2513 |
|
| 2514 |
def removeprefix(b, a): |
| 2515 |
b.wait() |
| 2516 |
assert not a.removeprefix(b'0') |
| 2517 |
|
| 2518 |
def removesuffix(b, a): |
| 2519 |
b.wait() |
| 2520 |
assert not a.removesuffix(b'0') |
| 2521 |
|
| 2522 |
def rfind(b, a): |
| 2523 |
b.wait() |
| 2524 |
assert a.rfind(b'\xdd') == -1 |
| 2525 |
|
| 2526 |
def rindex(b, a): |
| 2527 |
b.wait() |
| 2528 |
try: a.rindex(b'\xdd') |
| 2529 |
except ValueError: return |
| 2530 |
assert False |
| 2531 |
|
| 2532 |
def rpartition(b, a): |
| 2533 |
b.wait() |
| 2534 |
assert not a.rpartition(b'\xdd')[0] |
| 2535 |
|
| 2536 |
def rsplit(b, a): |
| 2537 |
b.wait() |
| 2538 |
assert len(a.rsplit(b'\xdd')) == 1 |
| 2539 |
|
| 2540 |
def rstrip(b, a): |
| 2541 |
b.wait() |
| 2542 |
assert not a.rstrip(b'0') |
| 2543 |
|
| 2544 |
def split(b, a): |
| 2545 |
b.wait() |
| 2546 |
assert len(a.split(b'\xdd')) == 1 |
| 2547 |
|
| 2548 |
def splitlines(b, a): |
| 2549 |
b.wait() |
| 2550 |
l = len(a.splitlines()) |
| 2551 |
assert l > 1 or l == 0 |
| 2552 |
|
| 2553 |
def startswith(b, a): |
| 2554 |
b.wait() |
| 2555 |
assert not a.startswith(b'\xdd') |
| 2556 |
|
| 2557 |
def strip(b, a): |
| 2558 |
b.wait() |
| 2559 |
assert not a.strip(b'0') |
| 2560 |
|
| 2561 |
def repeat(b, a): |
| 2562 |
b.wait() |
| 2563 |
a * 2 |
| 2564 |
|
| 2565 |
def contains(b, a): |
| 2566 |
b.wait() |
| 2567 |
assert 0xdd not in a |
| 2568 |
|
| 2569 |
def iconcat(b, a): # MODIFIES! |
| 2570 |
c = bytearray(b'0' * 0x400000) |
| 2571 |
b.wait() |
| 2572 |
a += c |
| 2573 |
|
| 2574 |
def irepeat(b, a): # MODIFIES! |
| 2575 |
b.wait() |
| 2576 |
a *= 2 |
| 2577 |
|
| 2578 |
def subscript(b, a): |
| 2579 |
b.wait() |
| 2580 |
try: assert a[0] != 0xdd |
| 2581 |
except IndexError: pass |
| 2582 |
|
| 2583 |
def ass_subscript(b, a): # MODIFIES! |
| 2584 |
c = bytearray(b'0' * 0x400000) |
| 2585 |
b.wait() |
| 2586 |
a[:] = c |
| 2587 |
|
| 2588 |
def ass_subscript2(b, a, c): # MODIFIES! |
| 2589 |
b.wait() |
| 2590 |
a[:] = c |
| 2591 |
assert b'\xdd' not in a |
| 2592 |
|
| 2593 |
def mod(b, a): |
| 2594 |
c = tuple(range(4096)) |
| 2595 |
b.wait() |
| 2596 |
try: a % c |
| 2597 |
except TypeError: pass |
| 2598 |
|
| 2599 |
def mod2(b, a, c): |
| 2600 |
b.wait() |
| 2601 |
d = a % c |
| 2602 |
assert b'\xdd' not in d |
| 2603 |
|
| 2604 |
def repr_(b, a): |
| 2605 |
b.wait() |
| 2606 |
repr(a) |
| 2607 |
|
| 2608 |
def capitalize(b, a): |
| 2609 |
b.wait() |
| 2610 |
c = a.capitalize() |
| 2611 |
assert not c or c[0] not in (0xdd, 0xcd) |
| 2612 |
|
| 2613 |
def center(b, a): |
| 2614 |
b.wait() |
| 2615 |
c = a.center(0x60000) |
| 2616 |
assert not c or c[0x20000] not in (0xdd, 0xcd) |
| 2617 |
|
| 2618 |
def expandtabs(b, a): |
| 2619 |
b.wait() |
| 2620 |
c = a.expandtabs() |
| 2621 |
assert not c or c[0] not in (0xdd, 0xcd) |
| 2622 |
|
| 2623 |
def ljust(b, a): |
| 2624 |
b.wait() |
| 2625 |
c = a.ljust(0x600000) |
| 2626 |
assert not c or c[0] not in (0xdd, 0xcd) |
| 2627 |
|
| 2628 |
def lower(b, a): |
| 2629 |
b.wait() |
| 2630 |
c = a.lower() |
| 2631 |
assert not c or c[0] not in (0xdd, 0xcd) |
| 2632 |
|
| 2633 |
def rjust(b, a): |
| 2634 |
b.wait() |
| 2635 |
c = a.rjust(0x600000) |
| 2636 |
assert not c or c[-1] not in (0xdd, 0xcd) |
| 2637 |
|
| 2638 |
def swapcase(b, a): |
| 2639 |
b.wait() |
| 2640 |
c = a.swapcase() |
| 2641 |
assert not c or c[-1] not in (0xdd, 0xcd) |
| 2642 |
|
| 2643 |
def title(b, a): |
| 2644 |
b.wait() |
| 2645 |
c = a.title() |
| 2646 |
assert not c or c[-1] not in (0xdd, 0xcd) |
| 2647 |
|
| 2648 |
def upper(b, a): |
| 2649 |
b.wait() |
| 2650 |
c = a.upper() |
| 2651 |
assert not c or c[-1] not in (0xdd, 0xcd) |
| 2652 |
|
| 2653 |
def zfill(b, a): |
| 2654 |
b.wait() |
| 2655 |
c = a.zfill(0x400000) |
| 2656 |
assert not c or c[-1] not in (0xdd, 0xcd) |
| 2657 |
|
| 2658 |
def resize(b, a): # MODIFIES! |
| 2659 |
b.wait() |
| 2660 |
a.resize(10) |
| 2661 |
|
| 2662 |
def check(funcs, a=None, *args): |
| 2663 |
if a is None: |
| 2664 |
a = bytearray(b'0' * 0x400000) |
| 2665 |
|
| 2666 |
barrier = threading.Barrier(len(funcs)) |
| 2667 |
threads = [] |
| 2668 |
|
| 2669 |
for func in funcs: |
| 2670 |
thread = threading.Thread(target=func, args=(barrier, a, *args)) |
| 2671 |
|
| 2672 |
threads.append(thread) |
| 2673 |
|
| 2674 |
with threading_helper.start_threads(threads): |
| 2675 |
pass |
| 2676 |
|
| 2677 |
# hard errors |
| 2678 |
|
| 2679 |
check([clear] + [reduce] * 10) |
| 2680 |
check([clear] + [reduceex2] * 10) |
| 2681 |
check([clear] + [append1] * 10) |
| 2682 |
check([clear] * 10) |
| 2683 |
check([clear] + [count0] * 10) |
| 2684 |
check([clear] + [decode] * 10) |
| 2685 |
check([clear] + [extend] * 10) |
| 2686 |
check([clear] + [find] * 10) |
| 2687 |
check([clear] + [hex] * 10) |
| 2688 |
check([clear] + [insert1] * 10) |
| 2689 |
check([clear] + [join] * 10) |
| 2690 |
check([clear] + [pop1] * 10) |
| 2691 |
check([clear] + [remove] * 10) |
| 2692 |
check([clear] + [replace] * 10) |
| 2693 |
check([clear] + [reverse] * 10) |
| 2694 |
check([clear, clear2] + [maketrans] * 10, bytearray(range(128)), bytearray(range(128))) |
| 2695 |
check([clear] + [translate] * 10, None, bytearray.maketrans(bytearray(range(128)), bytearray(range(128)))) |
| 2696 |
|
| 2697 |
check([clear] + [repeat] * 10) |
| 2698 |
check([clear] + [iconcat] * 10) |
| 2699 |
check([clear] + [irepeat] * 10) |
| 2700 |
check([clear] + [ass_subscript] * 10) |
| 2701 |
check([clear] + [repr_] * 10) |
| 2702 |
|
| 2703 |
# value errors |
| 2704 |
|
| 2705 |
check([clear] + [reduceex3] * 10, bytearray(b'a' * 0x40000)) |
| 2706 |
check([clear] + [copy] * 10) |
| 2707 |
check([clear] + [endswith] * 10) |
| 2708 |
check([clear] + [index] * 10) |
| 2709 |
check([clear] + [lstrip] * 10) |
| 2710 |
check([clear] + [partition] * 10) |
| 2711 |
check([clear] + [removeprefix] * 10, bytearray(b'0')) |
| 2712 |
check([clear] + [removesuffix] * 10, bytearray(b'0')) |
| 2713 |
check([clear] + [rfind] * 10) |
| 2714 |
check([clear] + [rindex] * 10) |
| 2715 |
check([clear] + [rpartition] * 10) |
| 2716 |
check([clear] + [rsplit] * 10, bytearray(b'0' * 0x4000)) |
| 2717 |
check([clear] + [rstrip] * 10) |
| 2718 |
check([clear] + [split] * 10, bytearray(b'0' * 0x4000)) |
| 2719 |
check([clear] + [splitlines] * 10, bytearray(b'\n' * 0x400)) |
| 2720 |
check([clear] + [startswith] * 10) |
| 2721 |
check([clear] + [strip] * 10) |
| 2722 |
|
| 2723 |
check([clear] + [resize] * 10) |
| 2724 |
|
| 2725 |
check([clear] + [contains] * 10) |
| 2726 |
check([clear] + [subscript] * 10) |
| 2727 |
check([clear2] + [ass_subscript2] * 10, None, bytearray(b'0' * 0x400000)) |
| 2728 |
check([clear] + [mod] * 10, bytearray(b'%d' * 4096)) |
| 2729 |
check([clear2] + [mod2] * 10, bytearray(b'%s'), bytearray(b'0' * 0x400000)) |
| 2730 |
|
| 2731 |
check([clear] + [capitalize] * 10, bytearray(b'a' * 0x40000)) |
| 2732 |
check([clear] + [center] * 10, bytearray(b'a' * 0x40000)) |
| 2733 |
check([clear] + [expandtabs] * 10, bytearray(b'0\t' * 4096)) |
| 2734 |
check([clear] + [ljust] * 10, bytearray(b'0' * 0x400000)) |
| 2735 |
check([clear] + [lower] * 10, bytearray(b'A' * 0x400000)) |
| 2736 |
check([clear] + [rjust] * 10, bytearray(b'0' * 0x400000)) |
| 2737 |
check([clear] + [swapcase] * 10, bytearray(b'aA' * 0x200000)) |
| 2738 |
check([clear] + [title] * 10, bytearray(b'aA' * 0x200000)) |
| 2739 |
check([clear] + [upper] * 10, bytearray(b'a' * 0x400000)) |
| 2740 |
check([clear] + [zfill] * 10, bytearray(b'1' * 0x200000)) |
| 2741 |
|
| 2742 |
@unittest.skipUnless(support.Py_GIL_DISABLED, 'this test can only possibly fail with GIL disabled') |
| 2743 |
@threading_helper.reap_threads |
| 2744 |
@threading_helper.requires_working_threading() |
| 2745 |
def test_free_threading_bytearrayiter(self): |
| 2746 |
# Non-deterministic but good chance to fail if bytearrayiter is not free-threading safe. |
| 2747 |
# We are fishing for a "Assertion failed: object has negative ref count" and tsan races. |
| 2748 |
|
| 2749 |
def iter_next(b, it): |
| 2750 |
b.wait() |
| 2751 |
list(it) |
| 2752 |
|
| 2753 |
def iter_reduce(b, it): |
| 2754 |
b.wait() |
| 2755 |
it.__reduce__() |
| 2756 |
|
| 2757 |
def iter_setstate(b, it): |
| 2758 |
b.wait() |
| 2759 |
it.__setstate__(0) |
| 2760 |
|
| 2761 |
def check(funcs, it): |
| 2762 |
barrier = threading.Barrier(len(funcs)) |
| 2763 |
threads = [] |
| 2764 |
|
| 2765 |
for func in funcs: |
| 2766 |
thread = threading.Thread(target=func, args=(barrier, it)) |
| 2767 |
|
| 2768 |
threads.append(thread) |
| 2769 |
|
| 2770 |
with threading_helper.start_threads(threads): |
| 2771 |
pass |
| 2772 |
|
| 2773 |
for _ in range(10): |
| 2774 |
ba = bytearray(b'0' * 0x4000) # this is a load-bearing variable, do not remove |
| 2775 |
|
| 2776 |
check([iter_next] * 10, iter(ba)) |
| 2777 |
check([iter_next] + [iter_reduce] * 10, iter(ba)) # for tsan |
| 2778 |
check([iter_next] + [iter_setstate] * 10, iter(ba)) # for tsan |
| 2779 |
|
| 2780 |
@unittest.skipUnless(support.Py_GIL_DISABLED, 'this test can only possibly fail with GIL disabled') |
| 2781 |
@threading_helper.reap_threads |
| 2782 |
@threading_helper.requires_working_threading() |
| 2783 |
def test_free_threading_bytearray_resize(self): |
| 2784 |
def resize_stress(ba): |
| 2785 |
for _ in range(1000): |
| 2786 |
try: |
| 2787 |
ba.resize(1000) |
| 2788 |
ba.resize(1) |
| 2789 |
except (BufferError, ValueError): |
| 2790 |
pass |
| 2791 |
|
| 2792 |
ba = bytearray(100) |
| 2793 |
threads = [threading.Thread(target=resize_stress, args=(ba,)) for _ in range(4)] |
| 2794 |
with threading_helper.start_threads(threads): |
| 2795 |
pass |
| 2796 |
|
| 2797 |
if __name__ == "__main__": |
| 2798 |
unittest.main() |