| # 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] |
| 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 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] |
| from testutil import assertRaises |
| |
| # 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) |