blob: 139b1dac21efdf72f94ea7957191dae2fa8d401b [file]
# Bytes test cases (compile and run)
[case testBytesBasics]
# Note: Add tests for additional operations to testBytesOps or in a new test case
def f(x: bytes) -> bytes:
return x
def eq(a: bytes, b: bytes) -> bool:
return a == b
def neq(a: bytes, b: bytes) -> bool:
return a != b
[file driver.py]
from native import f, eq, neq
assert f(b'123') == b'123'
assert f(b'\x07 \x0b " \t \x7f \xf0') == b'\x07 \x0b " \t \x7f \xf0'
assert eq(b'123', b'123')
assert not eq(b'123', b'1234')
assert not eq(b'123', b'124')
assert not eq(b'123', b'223')
assert neq(b'123', b'1234')
try:
f('x')
assert False
except TypeError:
pass
[case testBytesInit]
def test_bytes_init() -> None:
b1 = bytes([5])
assert b1 == b'\x05'
b2 = bytes([5, 10, 12])
assert b2 == b'\x05\n\x0c'
b3 = bytes(bytearray(b'foo'))
assert b3 == b'foo'
b4 = bytes(b'aaa')
assert b4 == b'aaa'
b5 = bytes(5)
assert b5 == b'\x00\x00\x00\x00\x00'
try:
bytes('x')
assert False
except TypeError:
pass
[case testBytesOps]
from testutil import assertRaises
def test_indexing() -> None:
# Use bytes() to avoid constant folding
b = b'asdf' + bytes()
assert b[0] == 97
assert b[1] == 115
assert b[3] == 102
assert b[-1] == 102
b = b'\xae\x80\xfe\x15' + bytes()
assert b[0] == 174
assert b[1] == 128
assert b[2] == 254
assert b[3] == 21
assert b[-4] == 174
with assertRaises(IndexError, "index out of range"):
b[4]
with assertRaises(IndexError, "index out of range"):
b[-5]
with assertRaises(IndexError, "index out of range"):
b[2**26]
def test_concat() -> None:
b1 = b'123' + bytes()
b2 = b'456' + bytes()
assert b1 + b2 == b'123456'
b3 = b1 + b2
b3 = b3 + b1
assert b3 == b'123456123'
assert b1 == b'123'
assert b2 == b'456'
assert type(b1) == bytes
assert type(b2) == bytes
assert type(b3) == bytes
brr1 = bytearray(3)
brr2 = bytearray(range(5))
b4 = b1 + brr1
assert b4 == b'123\x00\x00\x00'
assert type(brr1) == bytearray
assert type(b4) == bytes
brr3 = brr1 + brr2
assert brr3 == bytearray(b'\x00\x00\x00\x00\x01\x02\x03\x04')
assert len(brr3) == 8
assert type(brr3) == bytearray
brr3 = brr3 + bytearray([10])
assert brr3 == bytearray(b'\x00\x00\x00\x00\x01\x02\x03\x04\n')
b5 = brr2 + b2
assert b5 == bytearray(b'\x00\x01\x02\x03\x04456')
assert type(b5) == bytearray
b6 = b2 + brr2
assert b6 == b'456\x00\x01\x02\x03\x04'
assert type(b6) == bytes
def test_join() -> None:
seq = (b'1', b'"', b'\xf0')
assert b'\x07'.join(seq) == b'1\x07"\x07\xf0'
assert b', '.join(()) == b''
assert b', '.join([bytes() + b'ab']) == b'ab'
assert b', '.join([bytes() + b'ab', b'cd']) == b'ab, cd'
def test_len() -> None:
# Use bytes() to avoid constant folding
b = b'foo' + bytes()
assert len(b) == 3
assert len(bytes()) == 0
def test_ord() -> None:
assert ord(b'a') == ord('a')
assert ord(b'a' + bytes()) == ord('a')
assert ord(b'\x00') == 0
assert ord(b'\x00' + bytes()) == 0
assert ord(b'\xfe') == 254
assert ord(b'\xfe' + bytes()) == 254
with assertRaises(TypeError):
ord(b'aa')
with assertRaises(TypeError):
ord(b'')
def test_ord_bytesarray() -> None:
assert ord(bytearray(b'a')) == ord('a')
assert ord(bytearray(b'\x00')) == 0
assert ord(bytearray(b'\xfe')) == 254
with assertRaises(TypeError):
ord(bytearray(b'aa'))
with assertRaises(TypeError):
ord(bytearray(b''))
def test_multiply() -> None:
# Use bytes() and int() to avoid constant folding
b = b'ab' + bytes()
zero = int()
one = 1 + zero
three = 3 + zero
neg_one = -1 + zero
assert b * zero == b''
assert b * one == b'ab'
assert b * three == b'ababab'
assert b * neg_one == b''
assert zero * b == b''
assert one * b == b'ab'
assert three * b == b'ababab'
assert neg_one * b == b''
# Test with empty bytes
empty = bytes()
five = 5 + zero
assert empty * five == b''
assert five * empty == b''
# Test with single byte
single = b'\xff' + bytes()
four = 4 + zero
assert single * four == b'\xff\xff\xff\xff'
assert four * single == b'\xff\xff\xff\xff'
# Test type preservation
two = 2 + zero
result = b * two
assert type(result) == bytes
def test_translate() -> None:
# Identity translation table (fast path - exact bytes)
identity = bytes(range(256))
b = b'hello world' + bytes()
assert b.translate(identity) == b'hello world'
# ROT13-like translation for lowercase letters
table = bytearray(range(256))
for i in range(ord('a'), ord('z') + 1):
table[i] = ord('a') + (i - ord('a') + 13) % 26
table_bytes = bytes(table)
assert b'hello'.translate(table_bytes) == b'uryyb'
assert (b'abc' + bytes()).translate(table_bytes) == b'nop'
# Test with special characters
assert b'\x00\x01\xff'.translate(identity) == b'\x00\x01\xff'
# Test with bytearray table (slow path - fallback to Python method)
bytearray_table = bytearray(range(256))
assert b'hello'.translate(bytearray_table) == b'hello'
# Modify bytearray table to uppercase
for i in range(ord('a'), ord('z') + 1):
bytearray_table[i] = ord('A') + (i - ord('a'))
assert b'hello world'.translate(bytearray_table) == b'HELLO WORLD'
assert (b'test' + bytes()).translate(bytearray_table) == b'TEST'
# Test error on wrong table size
with assertRaises(ValueError, "translation table must be 256 characters long"):
b'test'.translate(b'short')
with assertRaises(ValueError, "translation table must be 256 characters long"):
b'test'.translate(bytes(100))
def test_startswith() -> None:
# Test default behavior
test = b'some string'
assert test.startswith(b'some')
assert test.startswith(b'some string')
assert not test.startswith(b'other')
assert not test.startswith(b'some string but longer')
# Test empty cases
assert test.startswith(b'')
assert b''.startswith(b'')
assert not b''.startswith(test)
# Test bytearray to verify slow paths
assert test.startswith(bytearray(b'some'))
assert not test.startswith(bytearray(b'other'))
test2 = bytearray(b'some string')
assert test2.startswith(b'some')
assert not test2.startswith(b'other')
def test_endswith() -> None:
# Test default behavior
test = b'some string'
assert test.endswith(b'string')
assert test.endswith(b'some string')
assert not test.endswith(b'other')
assert not test.endswith(b'some string but longer')
# Test empty cases
assert test.endswith(b'')
assert b''.endswith(b'')
assert not b''.endswith(test)
# Test bytearray to verify slow paths
assert test.endswith(bytearray(b'string'))
assert not test.endswith(bytearray(b'other'))
test2 = bytearray(b'some string')
assert test2.endswith(b'string')
assert not test2.endswith(b'other')
[case testBytesSlicing]
from typing import Any, cast
def test_reversed_slice_bounds() -> None:
bounds = (-100, -3, -1, 0, 1, 2, 3, 100, 2**100)
for value in (b"", b"a", b"abc"):
interpreted = cast(Any, value)
for start in bounds:
for end in bounds:
assert value[start:end] == interpreted[start:end]
def test_bytes_slicing() -> None:
b = b'abcdefg'
zero = int()
ten = 10 + zero
two = 2 + zero
five = 5 + zero
seven = 7 + zero
assert b[:ten] == b'abcdefg'
assert b[0:seven] == b'abcdefg'
assert b[0:(len(b)+1)] == b'abcdefg'
assert b[two:five] == b'cde'
assert b[two:two] == b''
assert b[-two:-two] == b''
assert b[-ten:(-ten+1)] == b''
assert b[:-two] == b'abcde'
assert b[:two] == b'ab'
assert b[:] == b'abcdefg'
assert b[-two:] == b'fg'
assert b[zero:] == b'abcdefg'
assert b[:zero] == b''
assert b[-ten:] == b'abcdefg'
assert b[-ten:ten] == b'abcdefg'
big_ints = [1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000, 2**24, 2**63]
for big_int in big_ints:
assert b[1:big_int] == b'bcdefg'
assert b[big_int:] == b''
assert b[-big_int:-1] == b'abcdef'
assert b[-big_int:big_int] == b'abcdefg'
assert type(b[-big_int:-1]) == bytes
assert type(b[-ten:]) == bytes
assert type(b[:]) == bytes
[case testBytearrayFromBytesSlice]
from typing import Any, Callable
from mypy_extensions import i32, i64
def from_slice(b: bytes, start: int, end: int) -> bytearray:
return bytearray(b[start:end])
def from_slice_i64(b: bytes, start: i64, end: i64) -> bytearray:
return bytearray(b[start:end])
def from_slice_i32(b: bytes, start: i32, end: i32) -> bytearray:
return bytearray(b[start:end])
def from_slice_calls(
source: Callable[[], bytes], start: Callable[[], int], end: Callable[[], int]
) -> bytearray:
return bytearray(source()[start():end()])
def from_slice_step(b: bytes, start: int, end: int, step: int) -> bytearray:
return bytearray(b[start:end:step])
def from_slice_any(b: bytes, start: Any, end: Any) -> bytearray:
return bytearray(b[start:end])
def from_start(b: bytes, start: int) -> bytearray:
return bytearray(b[start:])
def to_end(b: bytes, end: int) -> bytearray:
return bytearray(b[:end])
def from_start_i64(b: bytes, start: i64) -> bytearray:
return bytearray(b[start:])
def to_end_i32(b: bytes, end: i32) -> bytearray:
return bytearray(b[:end])
def full(b: bytes) -> bytearray:
return bytearray(b[:])
def from_start_calls(source: Callable[[], bytes], start: Callable[[], int]) -> bytearray:
return bytearray(source()[start():])
def to_end_calls(source: Callable[[], bytes], end: Callable[[], int]) -> bytearray:
return bytearray(source()[:end()])
[file driver.py]
from native import (
from_slice, from_slice_i64, from_slice_i32, from_slice_calls,
from_slice_step, from_slice_any,
from_start, to_end, from_start_i64, to_end_i32, full,
from_start_calls, to_end_calls,
)
from testutil import assertRaises
small_indices = [-100, -8, -7, -5, -3, -1, 0, 1, 3, 6, 7, 8, 100]
for f, large_indices in [
(from_slice, [-2**100, -2**63, 2**63, 2**100]),
(from_slice_i64, [-2**63, 2**63 - 1]),
(from_slice_i32, [-2**31, 2**31 - 1]),
]:
for b in [b'', b'abcdefg', b'\x00\xff\x80\x00abc']:
for start in small_indices + large_indices:
for end in small_indices + large_indices:
result = f(b, start, end)
assert type(result) is bytearray
assert result == bytearray(b[start:end]), (b, start, end, result)
assert result is not f(b, start, end)
for f, is_start, large_indices in [
(from_start, True, [-2**100, -2**63, 2**63, 2**100]),
(to_end, False, [-2**100, -2**63, 2**63, 2**100]),
(from_start_i64, True, [-2**63, 2**63 - 1]),
(to_end_i32, False, [-2**31, 2**31 - 1]),
]:
for b in [b'', b'abcdefg', b'\x00\xff\x80\x00abc']:
for n in small_indices + large_indices:
result = f(b, n)
expected = bytearray(b[n:] if is_start else b[:n])
assert type(result) is bytearray
assert result == expected, (b, n, result)
assert result is not f(b, n)
for b in [b'', b'abcdefg', b'\x00\xff\x80']:
result = full(b)
assert type(result) is bytearray
assert result == b
assert result is not full(b)
if result:
result[0] = 1
assert full(b) == b
b = b'abcdefg'
result = from_slice(b, 1, 4)
result[0] = 0
assert b == b'abcdefg'
assert from_slice(b, 1, 4) == b'bcd'
# A bytes subclass can override slicing, including returning a different type.
# Omitted bounds must remain None in a subclass's __getitem__.
class SubBytes(bytes):
def __getitem__(self, key):
assert key == expected_slice
return [0, 255, 128]
class BadSlice(bytes):
def __getitem__(self, key):
raise ValueError('slice failed')
class BadResult(bytes):
def __getitem__(self, key):
return object()
cases = [(from_slice, (start, end), slice(start, end))
for start, end in [(1, 4), (-5, -1), (0, 2**100)]]
cases.append((full, (), slice(None)))
for n in small_indices + [-2**100, 2**100]:
cases.extend([(from_start, (n,), slice(n, None)), (to_end, (n,), slice(None, n))])
for f, args, expected_slice in cases:
assert f(SubBytes(b), *args) == bytearray([0, 255, 128])
with assertRaises(ValueError, 'slice failed'):
f(BadSlice(b), *args)
with assertRaises(TypeError):
f(BadResult(b), *args)
# Evaluate the source and both bounds exactly once, in order, on either path.
def argument(name, value, fail=False):
def call():
calls.append(name)
if fail:
raise ValueError(name)
return value
return call
for start, end in [(1, 4), (1, -1), (-5, 4), (-5, -1), (-5, 2**100)]:
calls = []
assert from_slice_calls(
argument('source', b), argument('start', start), argument('end', end)
) == bytearray(b[start:end])
assert calls == ['source', 'start', 'end']
for fail_at in range(3):
calls = []
names = ['source', 'start', 'end']
args = [argument(name, value, i == fail_at)
for i, (name, value) in enumerate(zip(names, [b, 1, 4]))]
with assertRaises(ValueError, names[fail_at]):
from_slice_calls(*args)
assert calls == names[:fail_at + 1]
for f, direct in [(from_start_calls, from_start), (to_end_calls, to_end)]:
for n in [2, -2, 2**100]:
for fail in [False, True]:
calls = []
args = [argument('source', b), argument('bound', n, fail)]
if fail:
with assertRaises(ValueError, 'bound'):
f(*args)
else:
assert f(*args) == direct(b, n)
assert calls == ['source', 'bound']
# Unsupported forms must retain the ordinary constructor and slicing behavior.
for step in [-2, -1, 1, 2]:
assert from_slice_step(b, 1, 6, step) == bytearray(b[1:6:step])
with assertRaises(ValueError):
from_slice_step(b, 1, 6, 0)
class Index:
def __index__(self):
return 2
assert from_slice_any(b, Index(), 5) == b'cde'
assert from_slice_any(b, None, None) == b
with assertRaises(TypeError):
from_slice_any(b, 'bad', 5)
[case testBytearrayBasics]
from typing import Any
from testutil import assertRaises
def test_basics() -> None:
brr0 = bytearray()
assert brr0 == bytearray(b'')
assert brr0 == b''
assert len(brr0) == 0
brr1 = bytearray(3)
assert brr1 == bytearray(b'\x00\x00\x00')
assert brr1 == b'\x00\x00\x00'
l = [10, 20, 30, 40]
brr2 = bytearray(l)
assert brr2 == bytearray(b'\n\x14\x1e(')
assert brr2 == b'\n\x14\x1e('
brr3 = bytearray(range(5))
assert brr3 == bytearray(b'\x00\x01\x02\x03\x04')
assert brr3 == b'\x00\x01\x02\x03\x04'
brr4 = bytearray('string', 'utf-8')
assert brr4 == bytearray(b'string')
assert brr4 == b'string'
assert len(brr1) == 3
assert len(brr2) == 4
def test_bytearray_type_object() -> None:
b = bytearray()
assert type(b) is bytearray
def fb(b: bytes) -> str:
return "xy"
def test_bytearray_passed_into_bytes() -> None:
brr1: Any = bytearray()
with assertRaises(TypeError, "bytes object expected; got bytearray"):
fb(brr1)
with assertRaises(TypeError, "bytes object expected; got bytearray"):
b: bytes = brr1
def fba(brr: bytearray) -> str:
return "xy"
def test_bytes_passed_into_bytearray() -> None:
b: Any = b"foo"
with assertRaises(TypeError, "bytearray object expected; got bytes"):
fba(b)
with assertRaises(TypeError, "bytearray object expected; got bytes"):
brr: bytearray = b
[case testBytearraySlicing]
def test_bytearray_slicing() -> None:
b = bytearray(b'abcdefg')
zero = int()
ten = 10 + zero
two = 2 + zero
five = 5 + zero
seven = 7 + zero
assert b[:ten] == b'abcdefg'
assert b[0:seven] == b'abcdefg'
assert b[two:five] == b'cde'
assert b[two:two] == b''
assert b[-two:-two] == b''
assert b[-ten:(-ten+1)] == b''
assert b[:-two] == b'abcde'
assert b[:two] == b'ab'
assert b[:] == b'abcdefg'
assert b[-two:] == b'fg'
assert b[zero:] == b'abcdefg'
assert b[:zero] == b''
assert b[-ten:] == b'abcdefg'
assert b[-ten:ten] == b'abcdefg'
big_ints = [1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000, 2**24, 2**63]
for big_int in big_ints:
assert b[1:big_int] == b'bcdefg'
assert b[big_int:] == b''
assert b[-big_int:-1] == b'abcdef'
assert b[-big_int:big_int] == b'abcdefg'
assert type(b[-big_int:-1]) == bytearray
assert type(b[-ten:]) == bytearray
assert type(b[:]) == bytearray
[case testBytearrayIndexing]
from testutil import assertRaises
def test_bytearray_indexing() -> None:
b = bytearray(b'\xae\x80\xfe\x15')
assert b[0] == 174
assert b[1] == 128
assert b[2] == 254
assert b[3] == 21
assert b[-4] == 174
with assertRaises(IndexError, "index out of range"):
b[4]
with assertRaises(IndexError, "index out of range"):
b[-5]
b2 = bytearray([175, 255, 128, 22])
assert b2[0] == 175
assert b2[1] == 255
assert b2[-1] == 22
assert b2[2] == 128
with assertRaises(ValueError, "byte must be in range(0, 256)"):
b2[0] = -1
with assertRaises(ValueError, "byte must be in range(0, 256)"):
b2[0] = 256
[case testBytesIteration]
from typing import Iterator
def loop(b: bytes) -> list[int]:
a = []
for c in b:
a.append(c)
return a
def comprehension(b: bytes) -> list[int]:
return [c for c in b]
def test_bytes() -> None:
for b in [b'', b'a', b'\x00\x80\xff', bytes(range(256))]:
assert loop(b) == list(b)
assert comprehension(b) == list(b)
def test_bytes_subclass() -> None:
from bytessub import BytesSubclass
b = BytesSubclass(b'ab')
assert loop(b) == [97, 98]
assert comprehension(b) == [97, 98]
[file bytessub.py]
class BytesSubclass(bytes):
pass
[case testBytearrayIteration]
from typing import Iterator
def loop(b: bytearray) -> list[int]:
a = []
for c in b:
a.append(c)
return a
def comprehension(b: bytearray) -> list[int]:
return [c + 1 for c in b]
def gen_expr(b: bytearray) -> tuple[int, ...]:
return tuple(c * 2 for c in b)
def set_comprehension(b: bytearray) -> set[int]:
return {c for c in b}
def enum(b: bytearray) -> list[tuple[int, int]]:
return [(i, c) for i, c in enumerate(b)]
def zipped(b: bytearray, s: str) -> list[tuple[int, str]]:
return [(c, x) for c, x in zip(b, s)]
def generator(b: bytearray) -> Iterator[int]:
for c in b:
yield c
def test_basic() -> None:
for b in [bytearray(), bytearray(b'a'), bytearray(b'\x00\x80\xff'), bytearray(range(256))]:
assert loop(b) == list(b)
assert comprehension(b) == [c + 1 for c in list(b)]
assert gen_expr(b) == tuple(c * 2 for c in list(b))
assert set_comprehension(b) == set(list(b))
assert enum(b) == list(enumerate(list(b)))
assert zipped(b, 'xyz') == list(zip(list(b), 'xyz'))
assert list(generator(b)) == list(b)
def grow(b: bytearray) -> list[int]:
a = []
for c in b:
if len(b) < 5:
b.append(c + 10)
a.append(c)
return a
def grow_comprehension(b: bytearray) -> list[int]:
return [append_if_short(b, c) for c in b]
def append_if_short(b: bytearray, c: int) -> int:
if len(b) < 5:
b.append(c + 10)
return c
def shrink(b: bytearray) -> list[int]:
a = []
for c in b:
b.clear()
a.append(c)
return a
def shrink_comprehension(b: bytearray) -> list[int]:
return [clear(b, c) for c in b]
def clear(b: bytearray, c: int) -> int:
b.clear()
return c
def test_modify_while_iterating() -> None:
assert grow(bytearray(b'\x01\x02')) == [1, 2, 11, 12, 21]
assert grow_comprehension(bytearray(b'\x01\x02')) == [1, 2, 11, 12, 21]
assert shrink(bytearray(b'\x01\x02')) == [1]
assert shrink_comprehension(bytearray(b'\x01\x02')) == [1]
def test_subclass() -> None:
from bytearraysub import ByteArrayOverride, ByteArrayIterOverride
# Like CPython, ignore __len__ and __getitem__ overrides
b = ByteArrayOverride(b'ab')
assert loop(b) == [97, 98]
assert comprehension(b) == [98, 99]
b2 = ByteArrayIterOverride(b'ab')
assert loop(b2) == [42]
assert comprehension(b2) == [43]
[file bytearraysub.py]
from typing import Iterator
class ByteArrayOverride(bytearray):
def __len__(self) -> int:
return 1
def __getitem__(self, i: object) -> int: # type: ignore[override]
return 99
class ByteArrayIterOverride(bytearray):
def __iter__(self) -> Iterator[int]:
yield 42
[case testBytearrayPrimitivesMatchGeneric]
from typing import Any, Callable
from testutil import assertRaises
from basubclass import ByteArraySubclass, ByteArrayOverrides, WrongSliceType
BIG_INTS = [2**31, 2**62 - 1, 2**62, 2**63, 2**70]
def outcome(f: Callable[[], object]) -> object:
try:
return f()
except Exception as e:
return (type(e), str(e))
def test_len() -> None:
b = bytearray()
a: Any = b
for i in range(20):
assert len(b) == len(a) == i
a.append(i)
del a[3:7]
assert len(b) == len(a) == 16
a.extend(b"xyz")
assert len(b) == len(a) == 19
a.clear()
assert len(b) == len(a) == 0
def test_get_item() -> None:
b = bytearray(b"\x00\x7f\x80\xfe\xff")
a: Any = b
n = len(b)
for i in list(range(-n - 2, n + 2)) + BIG_INTS + [-x for x in BIG_INTS]:
assert outcome(lambda: b[i]) == outcome(lambda: a[i]), i
with assertRaises(IndexError, "bytearray index out of range"):
b[n + int()]
with assertRaises(IndexError, "bytearray index out of range"):
bytearray()[int()]
def test_set_item() -> None:
values = [-1, 0, 1, 127, 128, 255, 256] + BIG_INTS + [-x for x in BIG_INTS]
n = 4
for i in list(range(-n - 2, n + 2)) + BIG_INTS + [-x for x in BIG_INTS]:
for v in values:
b = bytearray(b"abcd")
a: Any = bytearray(b"abcd")
def set_native() -> None:
b[i] = v
def set_generic() -> None:
a[i] = v
assert outcome(set_native) == outcome(set_generic), (i, v)
assert b == a, (i, v)
b = bytearray(b"abcd")
# The value is checked before the index, like in CPython
with assertRaises(ValueError, "byte must be in range(0, 256)"):
b[10 + int()] = 256
with assertRaises(IndexError, "bytearray index out of range"):
b[10 + int()] = 255
b[1] += 1
b[-1] -= 2
assert b == b"accb"
def test_slice() -> None:
b = bytearray(b"abcdefg")
a: Any = b
n = len(b)
bounds = list(range(-n - 2, n + 3)) + BIG_INTS + [-x for x in BIG_INTS]
for i in bounds:
assert b[i:] == a[i:], i
assert b[:i] == a[:i], i
for j in bounds:
assert b[i:j] == a[i:j], (i, j)
assert type(b[i:j]) is bytearray
c = b[:]
assert c == b and c is not b
c[0] = ord("z")
assert b == b"abcdefg"
assert bytearray()[1:2] == bytearray()
def test_subclass() -> None:
# Use bytearray as the static type, so that the primitives are used
s: bytearray = ByteArraySubclass(b"abc")
assert len(s) == 3
assert s[1] == ord("b")
assert s[-1] == ord("c")
s[0] = ord("x")
assert s == b"xbc"
t = s[1:]
assert t == b"bc"
assert type(t) is bytearray
with assertRaises(IndexError, "bytearray index out of range"):
s[3 + int()]
overrides = ByteArrayOverrides(b"abc")
o: bytearray = overrides
assert o[0] == 42
assert o[-1] == 42
o[1] = 5
assert overrides.last_set == (1, 5)
assert o == b"abc"
assert o[0:2] == b"slice"
wrong = WrongSliceType(b"abc")
w: bytearray = wrong
wrong.result = 1.5
with assertRaises(TypeError, "bytearray object expected; got float"):
w[0:2]
wrong.result = b"bc"
with assertRaises(TypeError, "bytearray object expected; got bytes"):
w[1:]
wrong.result = ByteArraySubclass(b"bc")
assert w[1:] == b"bc"
[file basubclass.py]
from typing import Any
class ByteArraySubclass(bytearray):
pass
class ByteArrayOverrides(bytearray):
last_set: Any = None
def __getitem__(self, i: Any) -> Any:
if isinstance(i, slice):
return bytearray(b"slice")
return 42
def __setitem__(self, i: Any, v: Any) -> None:
self.last_set = (i, v)
class WrongSliceType(bytearray):
result: Any = None
def __getitem__(self, i: Any) -> Any:
if isinstance(i, slice):
return self.result
return super().__getitem__(i)
[case testBytesContains]
from typing import Any
from testutil import assertRaises
from bcsubclass import BytesSubclass, BytesContainsOverride
HAYSTACKS = [
b"",
b"a",
b"abc",
b"aaab",
b"\x00\x01\x7f\x80\xfe\xff",
b"abcabcabd" * 10,
bytes(range(256)),
b"x" * 40 + b"y",
]
def outcome(f: Any) -> Any:
try:
return f()
except Exception as e:
return type(e), str(e)
def needles(h: bytes) -> list[bytes]:
result = [b"", b"z", b"\x00", b"\xff", b"ab", b"abd", b"aab", h, h + b"!", b"!" + h]
for i in range(len(h)):
for j in range(i + 1, min(len(h), i + 35) + 1):
result.append(h[i:j])
# Same length, but differs in the last byte
result.append(h[i:j - 1] + bytes([(h[j - 1] + 1) % 256]))
return result
def test_int_in_bytes() -> None:
for h in HAYSTACKS:
a: Any = h
for n in list(range(-3, 260)) + [2**62, -2**62, 2**100, -2**100]:
an: Any = n
assert outcome(lambda: n in h) == outcome(lambda: an in a), (h, n)
assert outcome(lambda: n not in h) == outcome(lambda: an not in a), (h, n)
with assertRaises(ValueError, "byte must be in range(0, 256)"):
256 in b"a"
with assertRaises(ValueError, "byte must be in range(0, 256)"):
-1 not in b"a"
def test_bool_in_bytes() -> None:
t = True
f = False
assert t in b"\x01"
assert t not in b"\x00"
assert f in b"\x00"
assert f not in b"\x01"
def test_bytes_in_bytes() -> None:
for h in HAYSTACKS:
a: Any = h
for x in needles(h):
ax: Any = x
assert (x in h) == (ax in a), (h, x)
assert (x not in h) == (ax not in a), (h, x)
def test_long_needle() -> None:
h = b"a" * 100 + b"b"
for n in range(30, 40):
assert b"a" * n + b"b" in h
assert b"a" * n + b"c" not in h
assert b"b" + b"a" * n not in h
def test_frequent_first_byte() -> None:
# Dense false hits on the first needle byte switch to the CPython search
for first, last in [(b"a", b"b"), (b"\x00", b"\xff")]:
for size in [2, 32]:
needle = first * (size - 1) + last
h = first * 100
assert needle not in h
assert needle in h + needle
assert needle in h + needle + first * 20
# Just before, at, and after the first density check
for false_hits in [15, 16, 17]:
h = b"a" * false_hits
assert b"ab" in h + b"ab"
assert b"ab" not in h + b"a"
# The fallback must search beyond the dense prefix
h = b"a" * 100 + b"x" * 200
assert b"ab" in h + b"ab"
assert b"ab" not in h
def test_dense_and_sparse_regions() -> None:
# Sparse false hits keep the direct search going across multiple density checks.
# A subsequent dense region should still trigger the fallback.
for tail in [b"x" * 100, b"a" * 100]:
h = b"axxxxxxxx" * 32 + tail
assert b"ab" not in h
assert b"ab" in h + b"ab"
# At the first density check, the distance from the initial mark to the
# next candidate equals the position of the 16th false hit.
for distance in [127, 128, 129]:
h = b"a" * 15 + b"x" * (distance - 15) + b"ax"
assert b"ab" not in h
assert b"ab" in h + b"ab"
def test_subclass() -> None:
s: bytes = BytesSubclass(b"abc")
assert 97 in s
assert 100 not in s
assert b"bc" in s
assert BytesSubclass(b"bc") in b"abc"
assert BytesSubclass(b"bd") not in b"abc"
assert BytesSubclass(b"") in b""
o: bytes = BytesContainsOverride(b"abc")
assert 1 in o
assert 97 not in o
assert 1000 in o
assert b"zz" in o
assert b"ab" not in o
[file bcsubclass.py]
class BytesSubclass(bytes):
pass
class BytesContainsOverride(bytes):
def __contains__(self, x: object) -> bool:
return x == 1 or x == 1000 or x == b"zz"
[case testBytesJoin]
from typing import Any
from testutil import assertRaises
from a import bytes_subclass
def test_bytes_join() -> None:
assert b' '.join([b'a', b'b']) == b'a b'
assert b' '.join([]) == b''
y: bytes = bytes_subclass()
assert y.join([]) == b'spook'
n: Any = 5
with assertRaises(TypeError, "can only join an iterable"):
assert b' '.join(n)
[file a.py]
class bytes_subclass(bytes):
def join(self, iter):
return b'spook'
[case testBytesFormatting]
import sys
from typing import Any
from testutil import assertRaises
def format_int(n: int) -> bytes:
return b"%d" % n
def test_bytes_formatting_error_propagation() -> None:
sys_module: Any = sys
if hasattr(sys_module, "set_int_max_str_digits"):
old_limit = sys_module.get_int_max_str_digits()
try:
sys_module.set_int_max_str_digits(640)
n = 10 ** 1000
before = sys_module.getrefcount(n)
for _ in range(3):
with assertRaises(ValueError):
format_int(n)
assert sys_module.getrefcount(n) == before
assert n == 10 ** 1000 # Keep n alive across both refcount checks.
finally:
sys_module.set_int_max_str_digits(old_limit)
assert format_int(123) == b"123"
# https://www.python.org/dev/peps/pep-0461/
def test_bytes_formatting() -> None:
val = 10
assert b"%x" % val == b'a'
assert b'%4x' % val == b' a'
assert b'%#4x' % val == b' 0xa'
assert b'%04X' % val == b'000A'
assert b'%d' % val == b'10'
assert b'%d' % (val + 2) == b'12'
assert b'%d' % 0 == b'0'
assert b'%d' % -3 == b'-3'
assert b'folded: %d' % (4 - 2) == b'folded: 2'
large_num = 2**65
assert b'number: %d' % large_num == b'number: 36893488147419103232'
assert b'negative integer: %d' % (-large_num) == b'negative integer: -36893488147419103232'
assert b'%c' % 48 == b'0'
assert b'%c' % b'a' == b'a'
assert b'%c%c' % (48, b'a') == b'0a'
assert b'%b' % b'abc' == b'abc'
assert b'%b' % 'some string'.encode('utf8') == b'some string'
assert b'%a' % 3.14 == b'3.14'
assert b'%a' % b'abc' == b"b'abc'"
assert b'%a' % 'def' == b"'def'"
def test_bytes_formatting_2() -> None:
var = b'bb'
num = 10
assert b'aaa%bbbb%s' % (var, var) == b'aaabbbbbbb'
assert b'aaa%dbbb%b' % (num, var) == b'aaa10bbbbb'
assert b'%s%b' % (var, var) == b'bbbb'
assert b'%b' % bytes() == b''
assert b'%b' % b'' == b''
assert b'\xff%s' % b'\xff' == b'\xff\xff'
assert b'\xff%b' % '你好'.encode() == b'\xff\xe4\xbd\xa0\xe5\xa5\xbd'
aa = b'\xe4\xbd\xa0\xe5\xa5\xbd%b' % b'\xe4\xbd\xa0\xe5\xa5\xbd'
assert aa == b'\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd'
assert aa.decode() == '你好你好'
[typing fixtures/typing-full.pyi]
class A:
def __bytes__(self):
return b'aaa'
def test_bytes_dunder() -> None:
assert b'%b' % A() == b'aaa'
assert b'%s' % A() == b'aaa'
[case testIsInstance]
from copysubclass import subbytes, subbytearray
from typing import Any
def test_bytes() -> None:
b: Any = b''
assert isinstance(b, bytes)
assert isinstance(b + b'123', bytes)
assert isinstance(b + b'\xff', bytes)
assert isinstance(subbytes(), bytes)
assert isinstance(subbytes(b + b'123'), bytes)
assert isinstance(subbytes(b + b'\xff'), bytes)
assert not isinstance(set(), bytes)
assert not isinstance((), bytes)
assert not isinstance((b'1',b'2',b'3'), bytes)
assert not isinstance({b'a',b'b'}, bytes)
assert not isinstance(int() + 1, bytes)
assert not isinstance(str() + 'a', bytes)
def test_user_defined_bytes() -> None:
from userdefinedbytes import bytes
assert isinstance(bytes(), bytes)
assert not isinstance(b'\x7f', bytes)
def test_bytearray() -> None:
assert isinstance(bytearray(), bytearray)
assert isinstance(bytearray(b'123'), bytearray)
assert isinstance(bytearray(b'\xff'), bytearray)
assert isinstance(subbytearray(), bytearray)
assert isinstance(subbytearray(bytearray(b'123')), bytearray)
assert isinstance(subbytearray(bytearray(b'\xff')), bytearray)
assert not isinstance(set(), bytearray)
assert not isinstance((), bytearray)
assert not isinstance((bytearray(b'1'),bytearray(b'2'),bytearray(b'3')), bytearray)
assert not isinstance([bytearray(b'a'),bytearray(b'b')], bytearray)
assert not isinstance(int() + 1, bytearray)
assert not isinstance(str() + 'a', bytearray)
[file copysubclass.py]
class subbytes(bytes):
pass
class subbytearray(bytearray):
pass
[file userdefinedbytes.py]
class bytes:
pass
[case testBytesOptionalEquality]
from __future__ import annotations
def eq_b_opt_b(x: bytes | None, y: bytes) -> bool:
return x == y
def ne_b_b_opt(x: bytes, y: bytes | None) -> bool:
return x != y
def test_optional_eq() -> None:
b = b'x'
assert eq_b_opt_b(b, b)
assert eq_b_opt_b(b + bytes([int()]), b + bytes([int()]))
assert not eq_b_opt_b(b'x', b'y')
assert not eq_b_opt_b(b'y', b'x')
assert not eq_b_opt_b(None, b'x')
def test_optional_ne() -> None:
b = b'x'
assert not ne_b_b_opt(b, b)
assert not ne_b_b_opt(b + b'y', b + bytes() + b'y')
assert ne_b_b_opt(b'x', b'y')
assert ne_b_b_opt(b'y', b'x')
assert ne_b_b_opt(b'x', None)
[case testBytesOpsOverridingSubclass]
from testutil import assertRaises
from bytessub import BadBytes, GoodBytes
def mul(b: bytes, n: int) -> bytes:
return b * n
def rmul(b: bytes, n: int) -> bytes:
return n * b
def join(b: bytes) -> bytes:
return b.join([b'a', b'b'])
def translate(b: bytes, table: bytes) -> bytes:
return b.translate(table)
def get_slice(b: bytes, start: int, end: int) -> bytes:
return b[start:end]
BIG = 1 << 70
table = bytes(range(256))
def test_bad_result_type() -> None:
b: bytes = BadBytes(b'x')
with assertRaises(TypeError, "bytes object expected; got int"):
mul(b, 2)
with assertRaises(TypeError, "bytes object expected; got int"):
rmul(b, 2)
with assertRaises(TypeError, "bytes object expected; got int"):
join(b)
with assertRaises(TypeError, "bytes object expected; got int"):
translate(b, table)
with assertRaises(TypeError, "bytes object expected; got int"):
get_slice(b, 0, BIG)
with assertRaises(TypeError, "bytes object expected; got int"):
get_slice(b, -BIG, 1)
def test_overrides_called() -> None:
b: bytes = GoodBytes(b'x')
assert mul(b, 2) == b'xx!'
assert rmul(b, 2) == b'xx?'
assert type(rmul(b, 2)) is GoodBytes
assert join(b) == b'axb.'
assert translate(b, table) == b'x#'
assert get_slice(b, 0, BIG) == b'x^'
assert rmul(b'x', 2) == b'xx'
[file bytessub.py]
from typing import Any
class BadBytes(bytes):
def __mul__(self, n: Any) -> Any:
return 12345678901234567890
def __rmul__(self, n: Any) -> Any:
return 12345678901234567891
def join(self, it: Any) -> Any:
return 12345678901234567892
def translate(self, table: Any, delete: Any = b'') -> Any:
return 12345678901234567893
def __getitem__(self, i: Any) -> Any:
return 12345678901234567894
class GoodBytes(bytes):
def __mul__(self, n: Any) -> Any:
return bytes(self) * n + b'!'
def __rmul__(self, n: Any) -> Any:
return GoodBytes(n * bytes(self) + b'?')
def join(self, it: Any) -> Any:
return bytes(self).join(it) + b'.'
def translate(self, table: Any, delete: Any = b'') -> Any:
return bytes(self).translate(table) + b'#'
def __getitem__(self, i: Any) -> Any:
return bytes(self)[i] + b'^'