| 1 |
[case testDictGet] |
| 2 |
from typing import Dict |
| 3 |
def f(d: Dict[int, bool]) -> bool: |
| 4 |
return d[0] |
| 5 |
[out] |
| 6 |
def f(d): |
| 7 |
d :: dict |
| 8 |
r0, r1 :: object |
| 9 |
r2 :: bool |
| 10 |
L0: |
| 11 |
r0 = object 0 |
| 12 |
r1 = CPyDict_GetItem(d, r0) |
| 13 |
r2 = unbox(bool, r1) |
| 14 |
return r2 |
| 15 |
|
| 16 |
[case testDictSet] |
| 17 |
from typing import Dict |
| 18 |
def f(d: Dict[int, bool]) -> None: |
| 19 |
d[0] = False |
| 20 |
[out] |
| 21 |
def f(d): |
| 22 |
d :: dict |
| 23 |
r0, r1 :: object |
| 24 |
r2 :: i32 |
| 25 |
r3 :: bit |
| 26 |
L0: |
| 27 |
r0 = object 0 |
| 28 |
r1 = box(bool, 0) |
| 29 |
r2 = CPyDict_SetItem(d, r0, r1) |
| 30 |
r3 = r2 >= 0 :: signed |
| 31 |
return 1 |
| 32 |
|
| 33 |
[case testNewEmptyDict] |
| 34 |
from typing import Dict |
| 35 |
def f() -> None: |
| 36 |
d = {} # type: Dict[bool, int] |
| 37 |
[out] |
| 38 |
def f(): |
| 39 |
r0, d :: dict |
| 40 |
L0: |
| 41 |
r0 = PyDict_New() |
| 42 |
d = r0 |
| 43 |
return 1 |
| 44 |
|
| 45 |
[case testNewEmptyDictViaFunc] |
| 46 |
from typing import Dict |
| 47 |
def f() -> None: |
| 48 |
d: Dict[bool, int] = dict() |
| 49 |
|
| 50 |
[out] |
| 51 |
def f(): |
| 52 |
r0, d :: dict |
| 53 |
L0: |
| 54 |
r0 = PyDict_New() |
| 55 |
d = r0 |
| 56 |
return 1 |
| 57 |
|
| 58 |
[case testNewDictWithValues] |
| 59 |
def f(x: object) -> None: |
| 60 |
d = {1: 2, '': x} |
| 61 |
[out] |
| 62 |
def f(x): |
| 63 |
x :: object |
| 64 |
r0 :: str |
| 65 |
r1, r2 :: object |
| 66 |
r3, d :: dict |
| 67 |
L0: |
| 68 |
r0 = '' |
| 69 |
r1 = object 1 |
| 70 |
r2 = object 2 |
| 71 |
r3 = CPyDict_Build(2, r1, r2, r0, x) |
| 72 |
d = r3 |
| 73 |
return 1 |
| 74 |
|
| 75 |
[case testInDict] |
| 76 |
from typing import Dict |
| 77 |
def f(d: Dict[int, int]) -> bool: |
| 78 |
if 4 in d: |
| 79 |
return True |
| 80 |
else: |
| 81 |
return False |
| 82 |
[out] |
| 83 |
def f(d): |
| 84 |
d :: dict |
| 85 |
r0 :: object |
| 86 |
r1 :: i32 |
| 87 |
r2 :: bit |
| 88 |
r3 :: bool |
| 89 |
L0: |
| 90 |
r0 = object 4 |
| 91 |
r1 = PyDict_Contains(d, r0) |
| 92 |
r2 = r1 >= 0 :: signed |
| 93 |
r3 = truncate r1: i32 to builtins.bool |
| 94 |
if r3 goto L1 else goto L2 :: bool |
| 95 |
L1: |
| 96 |
return 1 |
| 97 |
L2: |
| 98 |
return 0 |
| 99 |
L3: |
| 100 |
unreachable |
| 101 |
|
| 102 |
[case testNotInDict] |
| 103 |
from typing import Dict |
| 104 |
def f(d: Dict[int, int]) -> bool: |
| 105 |
if 4 not in d: |
| 106 |
return True |
| 107 |
else: |
| 108 |
return False |
| 109 |
[out] |
| 110 |
def f(d): |
| 111 |
d :: dict |
| 112 |
r0 :: object |
| 113 |
r1 :: i32 |
| 114 |
r2 :: bit |
| 115 |
r3, r4 :: bool |
| 116 |
L0: |
| 117 |
r0 = object 4 |
| 118 |
r1 = PyDict_Contains(d, r0) |
| 119 |
r2 = r1 >= 0 :: signed |
| 120 |
r3 = truncate r1: i32 to builtins.bool |
| 121 |
r4 = r3 ^ 1 |
| 122 |
if r4 goto L1 else goto L2 :: bool |
| 123 |
L1: |
| 124 |
return 1 |
| 125 |
L2: |
| 126 |
return 0 |
| 127 |
L3: |
| 128 |
unreachable |
| 129 |
|
| 130 |
[case testDictUpdate] |
| 131 |
from typing import Dict |
| 132 |
def f(a: Dict[int, int], b: Dict[int, int]) -> None: |
| 133 |
a.update(b) |
| 134 |
[out] |
| 135 |
def f(a, b): |
| 136 |
a, b :: dict |
| 137 |
r0 :: i32 |
| 138 |
r1 :: bit |
| 139 |
L0: |
| 140 |
r0 = CPyDict_Update(a, b) |
| 141 |
r1 = r0 >= 0 :: signed |
| 142 |
return 1 |
| 143 |
|
| 144 |
[case testDictKeyLvalue] |
| 145 |
from typing import Dict |
| 146 |
def increment(d: Dict[str, int]) -> Dict[str, int]: |
| 147 |
for k in d: |
| 148 |
d[k] += 1 |
| 149 |
return d |
| 150 |
[out] |
| 151 |
def increment(d): |
| 152 |
d :: dict |
| 153 |
r0 :: short_int |
| 154 |
r1 :: native_int |
| 155 |
r2 :: object |
| 156 |
r3 :: tuple[bool, short_int, object] |
| 157 |
r4 :: short_int |
| 158 |
r5 :: bool |
| 159 |
r6 :: object |
| 160 |
r7, k :: str |
| 161 |
r8, r9, r10 :: object |
| 162 |
r11 :: i32 |
| 163 |
r12, r13, r14 :: bit |
| 164 |
L0: |
| 165 |
r0 = 0 |
| 166 |
r1 = PyDict_Size(d) |
| 167 |
r2 = CPyDict_GetKeysIter(d) |
| 168 |
L1: |
| 169 |
r3 = CPyDict_NextKey(r2, r0) |
| 170 |
r4 = r3[1] |
| 171 |
r0 = r4 |
| 172 |
r5 = r3[0] |
| 173 |
if r5 goto L2 else goto L4 :: bool |
| 174 |
L2: |
| 175 |
r6 = r3[2] |
| 176 |
r7 = cast(str, r6) |
| 177 |
k = r7 |
| 178 |
r8 = CPyDict_GetItem(d, k) |
| 179 |
r9 = object 1 |
| 180 |
r10 = PyNumber_InPlaceAdd(r8, r9) |
| 181 |
r11 = CPyDict_SetItem(d, k, r10) |
| 182 |
r12 = r11 >= 0 :: signed |
| 183 |
L3: |
| 184 |
r13 = CPyDict_CheckSize(d, r1) |
| 185 |
goto L1 |
| 186 |
L4: |
| 187 |
r14 = CPy_NoErrOccurred() |
| 188 |
L5: |
| 189 |
return d |
| 190 |
|
| 191 |
[case testDictDisplay] |
| 192 |
from typing import Dict |
| 193 |
def f(x: str, y: Dict[str, int]) -> Dict[str, int]: |
| 194 |
return {x: 2, **y, 'z': 3} |
| 195 |
[out] |
| 196 |
def f(x, y): |
| 197 |
x :: str |
| 198 |
y :: dict |
| 199 |
r0 :: str |
| 200 |
r1 :: object |
| 201 |
r2 :: dict |
| 202 |
r3 :: i32 |
| 203 |
r4 :: bit |
| 204 |
r5 :: object |
| 205 |
r6 :: i32 |
| 206 |
r7 :: bit |
| 207 |
L0: |
| 208 |
r0 = 'z' |
| 209 |
r1 = object 2 |
| 210 |
r2 = CPyDict_Build(1, x, r1) |
| 211 |
r3 = CPyDict_UpdateInDisplay(r2, y) |
| 212 |
r4 = r3 >= 0 :: signed |
| 213 |
r5 = object 3 |
| 214 |
r6 = CPyDict_SetItem(r2, r0, r5) |
| 215 |
r7 = r6 >= 0 :: signed |
| 216 |
return r2 |
| 217 |
|
| 218 |
[case testDictIterationMethods] |
| 219 |
from typing import Dict, TypedDict, Union |
| 220 |
|
| 221 |
class Person(TypedDict): |
| 222 |
name: str |
| 223 |
age: int |
| 224 |
|
| 225 |
def print_dict_methods(d1: Dict[int, int], d2: Dict[int, int]) -> None: |
| 226 |
for v in d1.values(): |
| 227 |
if v in d2: |
| 228 |
return |
| 229 |
for k, v in d2.items(): |
| 230 |
d2[k] += v |
| 231 |
def union_of_dicts(d: Union[Dict[str, int], Dict[str, str]]) -> None: |
| 232 |
new = {} |
| 233 |
for k, v in d.items(): |
| 234 |
new[k] = int(v) |
| 235 |
def typeddict(d: Person) -> None: |
| 236 |
for k, v in d.items(): |
| 237 |
if k == "name": |
| 238 |
name = v |
| 239 |
[typing fixtures/typing-full.pyi] |
| 240 |
[out] |
| 241 |
def print_dict_methods(d1, d2): |
| 242 |
d1, d2 :: dict |
| 243 |
r0 :: short_int |
| 244 |
r1 :: native_int |
| 245 |
r2 :: object |
| 246 |
r3 :: tuple[bool, short_int, object] |
| 247 |
r4 :: short_int |
| 248 |
r5 :: bool |
| 249 |
r6 :: object |
| 250 |
r7, v :: int |
| 251 |
r8 :: object |
| 252 |
r9 :: i32 |
| 253 |
r10 :: bit |
| 254 |
r11 :: bool |
| 255 |
r12, r13 :: bit |
| 256 |
r14 :: short_int |
| 257 |
r15 :: native_int |
| 258 |
r16 :: object |
| 259 |
r17 :: tuple[bool, short_int, object, object] |
| 260 |
r18 :: short_int |
| 261 |
r19 :: bool |
| 262 |
r20, r21 :: object |
| 263 |
r22, r23, k :: int |
| 264 |
r24, r25, r26, r27, r28 :: object |
| 265 |
r29 :: i32 |
| 266 |
r30, r31, r32 :: bit |
| 267 |
L0: |
| 268 |
r0 = 0 |
| 269 |
r1 = PyDict_Size(d1) |
| 270 |
r2 = CPyDict_GetValuesIter(d1) |
| 271 |
L1: |
| 272 |
r3 = CPyDict_NextValue(r2, r0) |
| 273 |
r4 = r3[1] |
| 274 |
r0 = r4 |
| 275 |
r5 = r3[0] |
| 276 |
if r5 goto L2 else goto L6 :: bool |
| 277 |
L2: |
| 278 |
r6 = r3[2] |
| 279 |
r7 = unbox(int, r6) |
| 280 |
v = r7 |
| 281 |
r8 = box(int, v) |
| 282 |
r9 = PyDict_Contains(d2, r8) |
| 283 |
r10 = r9 >= 0 :: signed |
| 284 |
r11 = truncate r9: i32 to builtins.bool |
| 285 |
if r11 goto L3 else goto L4 :: bool |
| 286 |
L3: |
| 287 |
return 1 |
| 288 |
L4: |
| 289 |
L5: |
| 290 |
r12 = CPyDict_CheckSize(d1, r1) |
| 291 |
goto L1 |
| 292 |
L6: |
| 293 |
r13 = CPy_NoErrOccurred() |
| 294 |
L7: |
| 295 |
r14 = 0 |
| 296 |
r15 = PyDict_Size(d2) |
| 297 |
r16 = CPyDict_GetItemsIter(d2) |
| 298 |
L8: |
| 299 |
r17 = CPyDict_NextItem(r16, r14) |
| 300 |
r18 = r17[1] |
| 301 |
r14 = r18 |
| 302 |
r19 = r17[0] |
| 303 |
if r19 goto L9 else goto L11 :: bool |
| 304 |
L9: |
| 305 |
r20 = r17[2] |
| 306 |
r21 = r17[3] |
| 307 |
r22 = unbox(int, r20) |
| 308 |
r23 = unbox(int, r21) |
| 309 |
k = r22 |
| 310 |
v = r23 |
| 311 |
r24 = box(int, k) |
| 312 |
r25 = CPyDict_GetItem(d2, r24) |
| 313 |
r26 = box(int, v) |
| 314 |
r27 = PyNumber_InPlaceAdd(r25, r26) |
| 315 |
r28 = box(int, k) |
| 316 |
r29 = CPyDict_SetItem(d2, r28, r27) |
| 317 |
r30 = r29 >= 0 :: signed |
| 318 |
L10: |
| 319 |
r31 = CPyDict_CheckSize(d2, r15) |
| 320 |
goto L8 |
| 321 |
L11: |
| 322 |
r32 = CPy_NoErrOccurred() |
| 323 |
L12: |
| 324 |
return 1 |
| 325 |
def union_of_dicts(d): |
| 326 |
d, r0, new :: dict |
| 327 |
r1 :: short_int |
| 328 |
r2 :: native_int |
| 329 |
r3 :: object |
| 330 |
r4 :: tuple[bool, short_int, object, object] |
| 331 |
r5 :: short_int |
| 332 |
r6 :: bool |
| 333 |
r7, r8 :: object |
| 334 |
r9 :: str |
| 335 |
r10 :: union[int, str] |
| 336 |
k :: str |
| 337 |
v :: union[int, str] |
| 338 |
r11 :: object |
| 339 |
r12 :: object[1] |
| 340 |
r13 :: object_ptr |
| 341 |
r14 :: object |
| 342 |
r15 :: int |
| 343 |
r16 :: object |
| 344 |
r17 :: i32 |
| 345 |
r18, r19, r20 :: bit |
| 346 |
L0: |
| 347 |
r0 = PyDict_New() |
| 348 |
new = r0 |
| 349 |
r1 = 0 |
| 350 |
r2 = PyDict_Size(d) |
| 351 |
r3 = CPyDict_GetItemsIter(d) |
| 352 |
L1: |
| 353 |
r4 = CPyDict_NextItem(r3, r1) |
| 354 |
r5 = r4[1] |
| 355 |
r1 = r5 |
| 356 |
r6 = r4[0] |
| 357 |
if r6 goto L2 else goto L4 :: bool |
| 358 |
L2: |
| 359 |
r7 = r4[2] |
| 360 |
r8 = r4[3] |
| 361 |
r9 = cast(str, r7) |
| 362 |
r10 = cast(union[int, str], r8) |
| 363 |
k = r9 |
| 364 |
v = r10 |
| 365 |
r11 = load_address PyLong_Type |
| 366 |
r12 = [v] |
| 367 |
r13 = load_address r12 |
| 368 |
r14 = PyObject_Vectorcall(r11, r13, 1, 0) |
| 369 |
keep_alive v |
| 370 |
r15 = unbox(int, r14) |
| 371 |
r16 = box(int, r15) |
| 372 |
r17 = CPyDict_SetItem(new, k, r16) |
| 373 |
r18 = r17 >= 0 :: signed |
| 374 |
L3: |
| 375 |
r19 = CPyDict_CheckSize(d, r2) |
| 376 |
goto L1 |
| 377 |
L4: |
| 378 |
r20 = CPy_NoErrOccurred() |
| 379 |
L5: |
| 380 |
return 1 |
| 381 |
def typeddict(d): |
| 382 |
d :: dict |
| 383 |
r0 :: short_int |
| 384 |
r1 :: native_int |
| 385 |
r2 :: object |
| 386 |
r3 :: tuple[bool, short_int, object, object] |
| 387 |
r4 :: short_int |
| 388 |
r5 :: bool |
| 389 |
r6, r7 :: object |
| 390 |
r8, k :: str |
| 391 |
v :: object |
| 392 |
r9 :: str |
| 393 |
r10 :: bool |
| 394 |
name :: object |
| 395 |
r11, r12 :: bit |
| 396 |
L0: |
| 397 |
r0 = 0 |
| 398 |
r1 = PyDict_Size(d) |
| 399 |
r2 = CPyDict_GetItemsIter(d) |
| 400 |
L1: |
| 401 |
r3 = CPyDict_NextItem(r2, r0) |
| 402 |
r4 = r3[1] |
| 403 |
r0 = r4 |
| 404 |
r5 = r3[0] |
| 405 |
if r5 goto L2 else goto L6 :: bool |
| 406 |
L2: |
| 407 |
r6 = r3[2] |
| 408 |
r7 = r3[3] |
| 409 |
r8 = cast(str, r6) |
| 410 |
k = r8 |
| 411 |
v = r7 |
| 412 |
r9 = 'name' |
| 413 |
r10 = CPyStr_EqualLiteral(k, r9, 4) |
| 414 |
if r10 goto L3 else goto L4 :: bool |
| 415 |
L3: |
| 416 |
name = v |
| 417 |
L4: |
| 418 |
L5: |
| 419 |
r11 = CPyDict_CheckSize(d, r1) |
| 420 |
goto L1 |
| 421 |
L6: |
| 422 |
r12 = CPy_NoErrOccurred() |
| 423 |
L7: |
| 424 |
return 1 |
| 425 |
|
| 426 |
[case testDictLoadAddress] |
| 427 |
def f() -> None: |
| 428 |
x = dict |
| 429 |
[out] |
| 430 |
def f(): |
| 431 |
r0, x :: object |
| 432 |
L0: |
| 433 |
r0 = load_address PyDict_Type |
| 434 |
x = r0 |
| 435 |
return 1 |
| 436 |
|
| 437 |
[case testDictClear] |
| 438 |
from typing import Dict |
| 439 |
def f(d: Dict[int, int]) -> None: |
| 440 |
return d.clear() |
| 441 |
[out] |
| 442 |
def f(d): |
| 443 |
d :: dict |
| 444 |
r0 :: bit |
| 445 |
L0: |
| 446 |
r0 = CPyDict_Clear(d) |
| 447 |
return 1 |
| 448 |
|
| 449 |
[case testDictCopy] |
| 450 |
from typing import Dict |
| 451 |
def f(d: Dict[int, int]) -> Dict[int, int]: |
| 452 |
return d.copy() |
| 453 |
[out] |
| 454 |
def f(d): |
| 455 |
d, r0 :: dict |
| 456 |
L0: |
| 457 |
r0 = CPyDict_Copy(d) |
| 458 |
return r0 |
| 459 |
|
| 460 |
[case testDictSetdefault] |
| 461 |
from typing import Dict |
| 462 |
def f(d: Dict[object, object]) -> object: |
| 463 |
return d.setdefault('a', 'b') |
| 464 |
|
| 465 |
def f2(d: Dict[object, object], flag: bool) -> object: |
| 466 |
if flag: |
| 467 |
return d.setdefault('a', set()) |
| 468 |
else: |
| 469 |
return d.setdefault('a', set('b')) |
| 470 |
|
| 471 |
def f3(d: Dict[object, object], flag: bool) -> object: |
| 472 |
if flag: |
| 473 |
return d.setdefault('a', []) |
| 474 |
else: |
| 475 |
return d.setdefault('a', [1]) |
| 476 |
|
| 477 |
def f4(d: Dict[object, object], flag: bool) -> object: |
| 478 |
if flag: |
| 479 |
return d.setdefault('a', {}) |
| 480 |
else: |
| 481 |
return d.setdefault('a', {'c': 1}) |
| 482 |
[out] |
| 483 |
def f(d): |
| 484 |
d :: dict |
| 485 |
r0, r1 :: str |
| 486 |
r2 :: object |
| 487 |
L0: |
| 488 |
r0 = 'a' |
| 489 |
r1 = 'b' |
| 490 |
r2 = CPyDict_SetDefault(d, r0, r1) |
| 491 |
return r2 |
| 492 |
def f2(d, flag): |
| 493 |
d :: dict |
| 494 |
flag :: bool |
| 495 |
r0 :: str |
| 496 |
r1 :: object |
| 497 |
r2, r3 :: str |
| 498 |
r4 :: set |
| 499 |
r5, r6 :: object |
| 500 |
L0: |
| 501 |
if flag goto L1 else goto L2 :: bool |
| 502 |
L1: |
| 503 |
r0 = 'a' |
| 504 |
r1 = CPyDict_SetDefaultWithEmptyDatatype(d, r0, 3) |
| 505 |
return r1 |
| 506 |
L2: |
| 507 |
r2 = 'a' |
| 508 |
r3 = 'b' |
| 509 |
r4 = PySet_New(r3) |
| 510 |
r5 = CPyDict_SetDefault(d, r2, r4) |
| 511 |
return r5 |
| 512 |
L3: |
| 513 |
r6 = box(None, 1) |
| 514 |
return r6 |
| 515 |
def f3(d, flag): |
| 516 |
d :: dict |
| 517 |
flag :: bool |
| 518 |
r0 :: str |
| 519 |
r1 :: object |
| 520 |
r2 :: str |
| 521 |
r3 :: list |
| 522 |
r4 :: object |
| 523 |
r5 :: ptr |
| 524 |
r6, r7 :: object |
| 525 |
L0: |
| 526 |
if flag goto L1 else goto L2 :: bool |
| 527 |
L1: |
| 528 |
r0 = 'a' |
| 529 |
r1 = CPyDict_SetDefaultWithEmptyDatatype(d, r0, 1) |
| 530 |
return r1 |
| 531 |
L2: |
| 532 |
r2 = 'a' |
| 533 |
r3 = PyList_New(1) |
| 534 |
r4 = object 1 |
| 535 |
r5 = list_items r3 |
| 536 |
buf_init_item r5, 0, r4 |
| 537 |
keep_alive r3 |
| 538 |
r6 = CPyDict_SetDefault(d, r2, r3) |
| 539 |
return r6 |
| 540 |
L3: |
| 541 |
r7 = box(None, 1) |
| 542 |
return r7 |
| 543 |
def f4(d, flag): |
| 544 |
d :: dict |
| 545 |
flag :: bool |
| 546 |
r0 :: str |
| 547 |
r1 :: object |
| 548 |
r2, r3 :: str |
| 549 |
r4 :: object |
| 550 |
r5 :: dict |
| 551 |
r6, r7 :: object |
| 552 |
L0: |
| 553 |
if flag goto L1 else goto L2 :: bool |
| 554 |
L1: |
| 555 |
r0 = 'a' |
| 556 |
r1 = CPyDict_SetDefaultWithEmptyDatatype(d, r0, 2) |
| 557 |
return r1 |
| 558 |
L2: |
| 559 |
r2 = 'a' |
| 560 |
r3 = 'c' |
| 561 |
r4 = object 1 |
| 562 |
r5 = CPyDict_Build(1, r3, r4) |
| 563 |
r6 = CPyDict_SetDefault(d, r2, r5) |
| 564 |
return r6 |
| 565 |
L3: |
| 566 |
r7 = box(None, 1) |
| 567 |
return r7 |