| 1 |
[case testBytesBasics] |
| 2 |
def f(num: int, l: list, d: dict, s: str) -> None: |
| 3 |
b1 = bytes() |
| 4 |
b2 = bytes(num) |
| 5 |
b3 = bytes(l) |
| 6 |
b4 = bytes(d) |
| 7 |
b5 = bytes(s) |
| 8 |
[out] |
| 9 |
def f(num, l, d, s): |
| 10 |
num :: int |
| 11 |
l :: list |
| 12 |
d :: dict |
| 13 |
s :: str |
| 14 |
r0, r1 :: object |
| 15 |
r2, b1 :: bytes |
| 16 |
r3, r4 :: object |
| 17 |
r5 :: object[1] |
| 18 |
r6 :: object_ptr |
| 19 |
r7 :: object |
| 20 |
r8, b2, r9, b3, r10, b4, r11, b5 :: bytes |
| 21 |
L0: |
| 22 |
r0 = load_address PyBytes_Type |
| 23 |
r1 = PyObject_Vectorcall(r0, 0, 0, 0) |
| 24 |
r2 = cast(bytes, r1) |
| 25 |
b1 = r2 |
| 26 |
r3 = load_address PyBytes_Type |
| 27 |
r4 = box(int, num) |
| 28 |
r5 = [r4] |
| 29 |
r6 = load_address r5 |
| 30 |
r7 = PyObject_Vectorcall(r3, r6, 1, 0) |
| 31 |
keep_alive r4 |
| 32 |
r8 = cast(bytes, r7) |
| 33 |
b2 = r8 |
| 34 |
r9 = PyBytes_FromObject(l) |
| 35 |
b3 = r9 |
| 36 |
r10 = PyBytes_FromObject(d) |
| 37 |
b4 = r10 |
| 38 |
r11 = PyBytes_FromObject(s) |
| 39 |
b5 = r11 |
| 40 |
return 1 |
| 41 |
|
| 42 |
[case testBytearrayBasics] |
| 43 |
def f(s: str, num: int) -> None: |
| 44 |
a = bytearray() |
| 45 |
b = bytearray(s) |
| 46 |
c = bytearray(num) |
| 47 |
[out] |
| 48 |
def f(s, num): |
| 49 |
s :: str |
| 50 |
num :: int |
| 51 |
r0, a, r1, b :: bytearray |
| 52 |
r2 :: object |
| 53 |
r3, c :: bytearray |
| 54 |
L0: |
| 55 |
r0 = CPyByteArray_New() |
| 56 |
a = r0 |
| 57 |
r1 = PyByteArray_FromObject(s) |
| 58 |
b = r1 |
| 59 |
r2 = box(int, num) |
| 60 |
r3 = PyByteArray_FromObject(r2) |
| 61 |
c = r3 |
| 62 |
return 1 |
| 63 |
|
| 64 |
[case testBytesEquality] |
| 65 |
def eq(x: bytes, y: bytes) -> bool: |
| 66 |
return x == y |
| 67 |
|
| 68 |
def neq(x: bytes, y: bytes) -> bool: |
| 69 |
return x != y |
| 70 |
[out] |
| 71 |
def eq(x, y): |
| 72 |
x, y :: bytes |
| 73 |
r0 :: i32 |
| 74 |
r1, r2 :: bit |
| 75 |
L0: |
| 76 |
r0 = CPyBytes_Compare(x, y) |
| 77 |
r1 = r0 >= 0 :: signed |
| 78 |
r2 = r0 == 1 |
| 79 |
return r2 |
| 80 |
def neq(x, y): |
| 81 |
x, y :: bytes |
| 82 |
r0 :: i32 |
| 83 |
r1, r2 :: bit |
| 84 |
L0: |
| 85 |
r0 = CPyBytes_Compare(x, y) |
| 86 |
r1 = r0 >= 0 :: signed |
| 87 |
r2 = r0 != 1 |
| 88 |
return r2 |
| 89 |
|
| 90 |
[case testBytesSlicing] |
| 91 |
def f(a: bytes, start: int, end: int) -> bytes: |
| 92 |
return a[start:end] |
| 93 |
[out] |
| 94 |
def f(a, start, end): |
| 95 |
a :: bytes |
| 96 |
start, end :: int |
| 97 |
r0 :: bytes |
| 98 |
L0: |
| 99 |
r0 = CPyBytes_GetSlice(a, start, end) |
| 100 |
return r0 |
| 101 |
|
| 102 |
[case testBytesIndex] |
| 103 |
from mypy_extensions import i64 |
| 104 |
|
| 105 |
def f(a: bytes, i: i64) -> int: |
| 106 |
return a[i] |
| 107 |
[out] |
| 108 |
def f(a, i): |
| 109 |
a :: bytes |
| 110 |
i, r0 :: i64 |
| 111 |
r1, r2 :: bool |
| 112 |
r3 :: int |
| 113 |
L0: |
| 114 |
r0 = CPyBytes_AdjustIndex(a, i) |
| 115 |
r1 = CPyBytes_RangeCheck(a, r0) |
| 116 |
if r1 goto L2 else goto L1 :: bool |
| 117 |
L1: |
| 118 |
r2 = raise IndexError('index out of range') |
| 119 |
unreachable |
| 120 |
L2: |
| 121 |
r3 = CPyBytes_GetItemUnsafe(a, r0) |
| 122 |
return r3 |
| 123 |
|
| 124 |
[case testBytesConcat] |
| 125 |
def f(a: bytes, b: bytes) -> bytes: |
| 126 |
return a + b |
| 127 |
[out] |
| 128 |
def f(a, b): |
| 129 |
a, b, r0 :: bytes |
| 130 |
L0: |
| 131 |
r0 = CPyBytes_Concat(a, b) |
| 132 |
return r0 |
| 133 |
|
| 134 |
[case testBytesJoin] |
| 135 |
from typing import List |
| 136 |
def f(b: List[bytes]) -> bytes: |
| 137 |
return b" ".join(b) |
| 138 |
[out] |
| 139 |
def f(b): |
| 140 |
b :: list |
| 141 |
r0, r1 :: bytes |
| 142 |
L0: |
| 143 |
r0 = b' ' |
| 144 |
r1 = CPyBytes_Join(r0, b) |
| 145 |
return r1 |
| 146 |
|
| 147 |
[case testBytesLen] |
| 148 |
def f(b: bytes) -> int: |
| 149 |
return len(b) |
| 150 |
[out] |
| 151 |
def f(b): |
| 152 |
b :: bytes |
| 153 |
r0 :: native_int |
| 154 |
r1 :: short_int |
| 155 |
L0: |
| 156 |
r0 = var_object_size b |
| 157 |
r1 = r0 << 1 |
| 158 |
return r1 |
| 159 |
|
| 160 |
[case testBytesFormatting] |
| 161 |
def f(var: bytes, num: int) -> None: |
| 162 |
b1 = b'aaaa%bbbbb%s' % (var, var) |
| 163 |
b2 = b'aaaa%bbbbb%s%d' % (var, var, num) |
| 164 |
b3 = b'%b' % var |
| 165 |
b4 = b'%ssss' % var |
| 166 |
b5 = b'%d' % num |
| 167 |
b6 = b'%d' % 42 |
| 168 |
b7 = b'%d' % (67 + 2) |
| 169 |
[typing fixtures/typing-full.pyi] |
| 170 |
[out] |
| 171 |
def f(var, num): |
| 172 |
var :: bytes |
| 173 |
num :: int |
| 174 |
r0, r1, r2, b1, r3, r4, r5, r6, b2, r7, b3, r8, r9, b4, r10, r11, b5, r12, r13, b6, r14, r15, b7 :: bytes |
| 175 |
L0: |
| 176 |
r0 = b'aaaa' |
| 177 |
r1 = b'bbbb' |
| 178 |
r2 = CPyBytes_Build(4, r0, var, r1, var) |
| 179 |
b1 = r2 |
| 180 |
r3 = CPyTagged_AsciiBytes(num) |
| 181 |
r4 = b'aaaa' |
| 182 |
r5 = b'bbbb' |
| 183 |
r6 = CPyBytes_Build(5, r4, var, r5, var, r3) |
| 184 |
b2 = r6 |
| 185 |
r7 = CPyBytes_Build(1, var) |
| 186 |
b3 = r7 |
| 187 |
r8 = b'sss' |
| 188 |
r9 = CPyBytes_Build(2, var, r8) |
| 189 |
b4 = r9 |
| 190 |
r10 = CPyTagged_AsciiBytes(num) |
| 191 |
r11 = CPyBytes_Build(1, r10) |
| 192 |
b5 = r11 |
| 193 |
r12 = b'42' |
| 194 |
r13 = CPyBytes_Build(1, r12) |
| 195 |
b6 = r13 |
| 196 |
r14 = b'69' |
| 197 |
r15 = CPyBytes_Build(1, r14) |
| 198 |
b7 = r15 |
| 199 |
return 1 |
| 200 |
|
| 201 |
[case testOptionalBytesEquality] |
| 202 |
from typing import Optional |
| 203 |
|
| 204 |
def non_opt_opt(x: bytes, y: Optional[bytes]) -> bool: |
| 205 |
return x != y |
| 206 |
[out] |
| 207 |
def non_opt_opt(x, y): |
| 208 |
x :: bytes |
| 209 |
y :: union[bytes, None] |
| 210 |
r0 :: object |
| 211 |
r1 :: bit |
| 212 |
r2 :: bool |
| 213 |
r3 :: bytes |
| 214 |
r4 :: i32 |
| 215 |
r5, r6 :: bit |
| 216 |
L0: |
| 217 |
r0 = load_address _Py_NoneStruct |
| 218 |
r1 = y == r0 |
| 219 |
if r1 goto L1 else goto L2 :: bool |
| 220 |
L1: |
| 221 |
r2 = 1 |
| 222 |
goto L3 |
| 223 |
L2: |
| 224 |
r3 = unchecked borrow cast(bytes, y) |
| 225 |
r4 = CPyBytes_Compare(r3, x) |
| 226 |
r5 = r4 >= 0 :: signed |
| 227 |
r6 = r4 != 1 |
| 228 |
r2 = r6 |
| 229 |
L3: |
| 230 |
keep_alive y |
| 231 |
return r2 |
| 232 |
|
| 233 |
[case testBytesMultiply] |
| 234 |
def b_times_i(s: bytes, n: int) -> bytes: |
| 235 |
return s * n |
| 236 |
def i_times_b(s: bytes, n: int) -> bytes: |
| 237 |
return n * s |
| 238 |
[out] |
| 239 |
def b_times_i(s, n): |
| 240 |
s :: bytes |
| 241 |
n :: int |
| 242 |
r0 :: bytes |
| 243 |
L0: |
| 244 |
r0 = CPyBytes_Multiply(s, n) |
| 245 |
return r0 |
| 246 |
def i_times_b(s, n): |
| 247 |
s :: bytes |
| 248 |
n :: int |
| 249 |
r0 :: bytes |
| 250 |
L0: |
| 251 |
r0 = CPyBytes_Multiply(s, n) |
| 252 |
return r0 |
| 253 |
|
| 254 |
[case testBytesTranslate] |
| 255 |
def f(b: bytes, table: bytes) -> bytes: |
| 256 |
return b.translate(table) |
| 257 |
[out] |
| 258 |
def f(b, table): |
| 259 |
b, table, r0 :: bytes |
| 260 |
L0: |
| 261 |
r0 = CPyBytes_Translate(b, table) |
| 262 |
return r0 |
| 263 |
|
| 264 |
[case testBytesStartsWith] |
| 265 |
def f(a: bytes, b: bytes) -> bool: |
| 266 |
return a.startswith(b) |
| 267 |
[out] |
| 268 |
def f(a, b): |
| 269 |
a, b :: bytes |
| 270 |
r0 :: i32 |
| 271 |
r1 :: bool |
| 272 |
L0: |
| 273 |
r0 = CPyBytes_Startswith(a, b) |
| 274 |
r1 = truncate r0: i32 to builtins.bool |
| 275 |
return r1 |
| 276 |
|
| 277 |
[case testBytesEndsWith] |
| 278 |
def f(a: bytes, b: bytes) -> bool: |
| 279 |
return a.endswith(b) |
| 280 |
[out] |
| 281 |
def f(a, b): |
| 282 |
a, b :: bytes |
| 283 |
r0 :: i32 |
| 284 |
r1 :: bool |
| 285 |
L0: |
| 286 |
r0 = CPyBytes_Endswith(a, b) |
| 287 |
r1 = truncate r0: i32 to builtins.bool |
| 288 |
return r1 |
| 289 |
|
| 290 |
[case testBytesVsBytearray] |
| 291 |
def bytes_func(b: bytes) -> None: pass |
| 292 |
def bytearray_func(ba: bytearray) -> None: pass |
| 293 |
|
| 294 |
def foo(b: bytes, ba: bytearray) -> None: |
| 295 |
bytes_func(b) |
| 296 |
bytearray_func(ba) |
| 297 |
bytes_func(ba) |
| 298 |
bytearray_func(b) |
| 299 |
[out] |
| 300 |
main:7: error: Argument 1 to "bytes_func" has incompatible type "bytearray"; expected "bytes" |
| 301 |
main:8: error: Argument 1 to "bytearray_func" has incompatible type "bytes"; expected "bytearray" |