[case testBytesBasics] def f(num: int, l: list, d: dict, s: str) -> None: b1 = bytes() b2 = bytes(num) b3 = bytes(l) b4 = bytes(d) b5 = bytes(s) [out] def f(num, l, d, s): num :: int l :: list d :: dict s :: str r0, r1 :: object r2, b1 :: bytes r3, r4 :: object r5 :: object[1] r6 :: object_ptr r7 :: object r8, b2, r9, b3, r10, b4, r11, b5 :: bytes L0: r0 = load_address PyBytes_Type r1 = PyObject_Vectorcall(r0, 0, 0, 0) r2 = cast(bytes, r1) b1 = r2 r3 = load_address PyBytes_Type r4 = box(int, num) r5 = [r4] r6 = load_address r5 r7 = PyObject_Vectorcall(r3, r6, 1, 0) keep_alive r4 r8 = cast(bytes, r7) b2 = r8 r9 = PyBytes_FromObject(l) b3 = r9 r10 = PyBytes_FromObject(d) b4 = r10 r11 = PyBytes_FromObject(s) b5 = r11 return 1 [case testBytearrayBasics] def f(s: str, num: int) -> None: a = bytearray() b = bytearray(s) c = bytearray(num) [out] def f(s, num): s :: str num :: int r0, a, r1, b :: bytearray r2 :: object r3, c :: bytearray L0: r0 = CPyByteArray_New() a = r0 r1 = PyByteArray_FromObject(s) b = r1 r2 = box(int, num) r3 = PyByteArray_FromObject(r2) c = r3 return 1 [case testBytesEquality] def eq(x: bytes, y: bytes) -> bool: return x == y def neq(x: bytes, y: bytes) -> bool: return x != y [out] def eq(x, y): x, y :: bytes r0 :: i32 r1, r2 :: bit L0: r0 = CPyBytes_Compare(x, y) r1 = r0 >= 0 :: signed r2 = r0 == 1 return r2 def neq(x, y): x, y :: bytes r0 :: i32 r1, r2 :: bit L0: r0 = CPyBytes_Compare(x, y) r1 = r0 >= 0 :: signed r2 = r0 != 1 return r2 [case testBytesSlicing] def f(a: bytes, start: int, end: int) -> bytes: return a[start:end] [out] def f(a, start, end): a :: bytes start, end :: int r0 :: bytes L0: r0 = CPyBytes_GetSlice(a, start, end) return r0 [case testBytesIndex] from mypy_extensions import i64 def f(a: bytes, i: i64) -> int: return a[i] [out] def f(a, i): a :: bytes i, r0 :: i64 r1, r2 :: bool r3 :: int L0: r0 = CPyBytes_AdjustIndex(a, i) r1 = CPyBytes_RangeCheck(a, r0) if r1 goto L2 else goto L1 :: bool L1: r2 = raise IndexError('index out of range') unreachable L2: r3 = CPyBytes_GetItemUnsafe(a, r0) return r3 [case testBytesConcat] def f(a: bytes, b: bytes) -> bytes: return a + b [out] def f(a, b): a, b, r0 :: bytes L0: r0 = CPyBytes_Concat(a, b) return r0 [case testBytesJoin] from typing import List def f(b: List[bytes]) -> bytes: return b" ".join(b) [out] def f(b): b :: list r0, r1 :: bytes L0: r0 = b' ' r1 = CPyBytes_Join(r0, b) return r1 [case testBytesLen] def f(b: bytes) -> int: return len(b) [out] def f(b): b :: bytes r0 :: native_int r1 :: short_int L0: r0 = var_object_size b r1 = r0 << 1 return r1 [case testBytesFormatting] def f(var: bytes, num: int) -> None: b1 = b'aaaa%bbbbb%s' % (var, var) b2 = b'aaaa%bbbbb%s%d' % (var, var, num) b3 = b'%b' % var b4 = b'%ssss' % var b5 = b'%d' % num b6 = b'%d' % 42 b7 = b'%d' % (67 + 2) [typing fixtures/typing-full.pyi] [out] def f(var, num): var :: bytes num :: int r0, r1, r2, b1, r3, r4, r5, r6, b2, r7, b3, r8, r9, b4, r10, r11, b5, r12, r13, b6, r14, r15, b7 :: bytes L0: r0 = b'aaaa' r1 = b'bbbb' r2 = CPyBytes_Build(4, r0, var, r1, var) b1 = r2 r3 = CPyTagged_AsciiBytes(num) r4 = b'aaaa' r5 = b'bbbb' r6 = CPyBytes_Build(5, r4, var, r5, var, r3) b2 = r6 r7 = CPyBytes_Build(1, var) b3 = r7 r8 = b'sss' r9 = CPyBytes_Build(2, var, r8) b4 = r9 r10 = CPyTagged_AsciiBytes(num) r11 = CPyBytes_Build(1, r10) b5 = r11 r12 = b'42' r13 = CPyBytes_Build(1, r12) b6 = r13 r14 = b'69' r15 = CPyBytes_Build(1, r14) b7 = r15 return 1 [case testOptionalBytesEquality] from typing import Optional def non_opt_opt(x: bytes, y: Optional[bytes]) -> bool: return x != y [out] def non_opt_opt(x, y): x :: bytes y :: union[bytes, None] r0 :: object r1 :: bit r2 :: bool r3 :: bytes r4 :: i32 r5, r6 :: bit L0: r0 = load_address _Py_NoneStruct r1 = y == r0 if r1 goto L1 else goto L2 :: bool L1: r2 = 1 goto L3 L2: r3 = unchecked borrow cast(bytes, y) r4 = CPyBytes_Compare(r3, x) r5 = r4 >= 0 :: signed r6 = r4 != 1 r2 = r6 L3: keep_alive y return r2 [case testBytesMultiply] def b_times_i(s: bytes, n: int) -> bytes: return s * n def i_times_b(s: bytes, n: int) -> bytes: return n * s [out] def b_times_i(s, n): s :: bytes n :: int r0 :: bytes L0: r0 = CPyBytes_Multiply(s, n) return r0 def i_times_b(s, n): s :: bytes n :: int r0 :: bytes L0: r0 = CPyBytes_Multiply(s, n) return r0 [case testBytesTranslate] def f(b: bytes, table: bytes) -> bytes: return b.translate(table) [out] def f(b, table): b, table, r0 :: bytes L0: r0 = CPyBytes_Translate(b, table) return r0 [case testBytesStartsWith] def f(a: bytes, b: bytes) -> bool: return a.startswith(b) [out] def f(a, b): a, b :: bytes r0 :: i32 r1 :: bool L0: r0 = CPyBytes_Startswith(a, b) r1 = truncate r0: i32 to builtins.bool return r1 [case testBytesEndsWith] def f(a: bytes, b: bytes) -> bool: return a.endswith(b) [out] def f(a, b): a, b :: bytes r0 :: i32 r1 :: bool L0: r0 = CPyBytes_Endswith(a, b) r1 = truncate r0: i32 to builtins.bool return r1 [case testBytesVsBytearray] def bytes_func(b: bytes) -> None: pass def bytearray_func(ba: bytearray) -> None: pass def foo(b: bytes, ba: bytearray) -> None: bytes_func(b) bytearray_func(ba) bytes_func(ba) bytearray_func(b) [out] main:7: error: Argument 1 to "bytes_func" has incompatible type "bytearray"; expected "bytes" main:8: error: Argument 1 to "bytearray_func" has incompatible type "bytes"; expected "bytearray"