monty 0.0.21

A sandboxed, snapshotable Python interpreter written in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
use std::{
    borrow::Cow,
    cmp::Ordering,
    fmt::{self, Write},
    mem::{self, discriminant},
    str::FromStr,
};

use num_bigint::{BigInt, Sign};
use num_traits::FromPrimitive;

use crate::{
    builtins::Builtins,
    bytecode::{CallResult, VM},
    defer_drop,
    exception_private::{ExcType, ExcTypeExt, RunResult, SimpleException},
    expressions::CmpOperator,
    fstring::FormatFloat,
    hash::{HashValue, hash_one, hash_python_long_int},
    heap::{ContainsHeap, DropWithContext, Heap, HeapData, HeapId, HeapReadOutput},
    heap_data::heap_subscript,
    identity::Identity,
    intern::{BytesId, FunctionId, Interns, LongIntId, StaticStrings, StringId},
    modules::ModuleFunctions,
    resource_checks::check_pow_size,
    types::{
        Bytes, BytesIterator, CmpOrder, LazyHeapSet, LongInt, Property, PyTrait, StringIterator, Type,
        bytes::{bytes_contains, bytes_repr_fmt, concat_bytes, get_byte_at_index, repeat_bytes},
        instance::{instance_dataclass_eq, instance_getattr, instance_repr_fmt, instance_str, instance_user_eq},
        long_int::{
            bigint_cmp_f64, bigint_cmp_i64, bigint_eq_f64, bigint_eq_i64, check_bits_str_digits_limit, i64_cmp_f64,
            repeat_count, wide_i128_into_value,
        },
        namedtuple::cmp_item_seqs,
        slice::slice_collect_iterator,
        str::{
            allocate_char, allocate_string, concat_allocate_str, get_char_at_index, repeat_str, str_contains,
            string_repr_fmt,
        },
    },
};

/// Primary value type representing Python objects at runtime.
///
/// This enum uses a hybrid design: small immediate values (Int, Bool, None) are stored
/// inline, while heap-allocated values (List, Str, Dict, etc.) are stored in the arena
/// and referenced via `Ref(HeapId)`.
///
/// NOTE: `Clone` is intentionally NOT derived. Use `clone_with_heap()`. Direct cloning via `.clone()` would
/// bypass reference counting and cause memory leaks.
///
/// NOTE: it's important to keep this size small to minimize memory overhead!
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub(crate) enum Value {
    // Immediate values (stored inline, no heap allocation)
    Undefined,
    Ellipsis,
    NotImplemented,
    None,
    Bool(bool),
    Int(i64),
    Float(f64),
    /// An interned string literal. The StringId references the string in the Interns table.
    /// To get the actual string content, use `interns.get(string_id)`.
    InternString(StringId),
    /// An interned bytes literal. The BytesId references the bytes in the Interns table.
    /// To get the actual bytes content, use `interns.get_bytes(bytes_id)`.
    InternBytes(BytesId),
    /// An interned long integer literal. The `LongIntId` references the `BigInt` in the Interns table.
    /// Used for integer literals exceeding i64 range. Converted to heap-allocated `LongInt` on load.
    InternLongInt(LongIntId),
    /// A builtin function or exception type
    Builtin(Builtins),
    /// A function from a module (not a global builtin).
    /// Module functions require importing a module to access (e.g., `asyncio.gather`).
    ModuleFunction(ModuleFunctions),
    /// A function defined in the module (not a closure, doesn't capture any variables)
    DefFunction(FunctionId),
    /// A marker value representing special objects like sys.stdout/stderr.
    /// These exist but have minimal functionality in the sandboxed environment.
    Marker(Marker),
    /// A property descriptor that computes its value when accessed.
    /// When retrieved via `py_getattr`, the property's getter is invoked.
    Property(Property),

    // Heap-allocated values (stored in arena)
    Ref(HeapId),

    /// Sentinel value indicating this Value was properly cleaned up via `drop_with`.
    /// Only exists when `memory-model-checks` feature is enabled. Used to verify reference counting
    /// correctness - if a `Ref` variant is dropped without calling `drop_with`, the
    /// Drop impl will panic.
    #[cfg(feature = "memory-model-checks")]
    Dereferenced,
}

/// Scoped view of a value that keeps a referenced heap entry open for repeated operations.
pub(crate) enum ValueRead<'h, 'v> {
    /// Immediate values need no heap access.
    Immediate(&'v Value),
    /// Heap values retain both their owner and the typed heap read handle.
    Heap {
        /// The `Value::Ref` this view was taken from. Keeps the entry alive for
        /// the view's lifetime, and supplies the `HeapId` that `py_next` needs
        /// to drive a user-defined `__next__`.
        owner: &'v Value,
        value: HeapReadOutput<'h>,
    },
}

impl<'h> ValueRead<'h, '_> {
    /// Advances this value without reacquiring its heap entry.
    ///
    /// This is the timeout boundary for Rust-side loops over retained iterators.
    /// Bytecode iteration dispatches directly after the VM's per-opcode check.
    pub(crate) fn py_next(&mut self, vm: &mut VM<'h>) -> RunResult<Option<Value>> {
        vm.heap.check_time()?;
        match self {
            Self::Immediate(value) => Err(ExcType::type_error_not_iterator(&value.py_type_name(vm))),
            Self::Heap { owner, value } => value.py_next(owner.ref_id(), vm),
        }
    }

    /// Returns the iterator's internal remaining-length hint when available.
    pub(crate) fn iter_size_hint(&self, vm: &VM<'h>) -> usize {
        match self {
            Self::Heap {
                value: HeapReadOutput::ListIterator(iter),
                ..
            } => iter.get(vm.heap).size_hint(vm.heap),
            Self::Heap {
                value: HeapReadOutput::DequeIterator(iter),
                ..
            } => iter.get(vm.heap).size_hint(vm.heap),
            Self::Heap {
                value: HeapReadOutput::TupleIterator(iter),
                ..
            } => iter.get(vm.heap).size_hint(vm.heap),
            Self::Heap {
                value: HeapReadOutput::StringIterator(iter),
                ..
            } => iter.get(vm.heap).size_hint(vm),
            Self::Heap {
                value: HeapReadOutput::BytesIterator(iter),
                ..
            } => iter.get(vm.heap).size_hint(vm),
            Self::Heap {
                value: HeapReadOutput::RangeIterator(iter),
                ..
            } => iter.get(vm.heap).size_hint(),
            Self::Heap {
                value: HeapReadOutput::DictKeyIterator(iter),
                ..
            } => iter.get(vm.heap).size_hint(),
            Self::Heap {
                value: HeapReadOutput::DictItemIterator(iter),
                ..
            } => iter.get(vm.heap).size_hint(),
            Self::Heap {
                value: HeapReadOutput::DictValueIterator(iter),
                ..
            } => iter.get(vm.heap).size_hint(),
            Self::Heap {
                value: HeapReadOutput::SetIterator(iter),
                ..
            } => iter.get(vm.heap).size_hint(),
            Self::Heap {
                value: HeapReadOutput::Itertools(iter),
                ..
            } => iter.get(vm.heap).size_hint(),
            _ => 0,
        }
    }
}

/// Size of a single `Value` slot in bytes.
///
/// Used to preflight operations that allocate many value slots at once.
pub(crate) const VALUE_SIZE: usize = mem::size_of::<Value>();

/// Borrowed integer payload used to format a Python `id()` in callable reprs.
enum PythonIdDisplay<'a> {
    /// Identity that fits Monty's immediate integer representation.
    Int(i64),
    /// Arbitrary-precision identity borrowed from its heap value.
    LongInt(&'a BigInt),
}

impl<'a> PythonIdDisplay<'a> {
    /// Extracts the integer payload returned by the structural identity encoder.
    fn new(value: &'a Value, heap: &'a Heap) -> Self {
        match value {
            Value::Int(value) => Self::Int(*value),
            Value::Ref(id) => match heap.get(*id) {
                HeapData::LongInt(value) => Self::LongInt(value.inner()),
                _ => unreachable!("identity values are integers"),
            },
            _ => unreachable!("identity values are integers"),
        }
    }
}

impl fmt::LowerHex for PythonIdDisplay<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Int(value) => fmt::LowerHex::fmt(value, f),
            Self::LongInt(value) => fmt::LowerHex::fmt(value, f),
        }
    }
}

/// Drop implementation that panics if a `Ref` variant is dropped without calling `drop_with`.
/// This helps catch reference counting bugs during development/testing.
/// Only enabled when the `memory-model-checks` feature is active.
#[cfg(feature = "memory-model-checks")]
impl Drop for Value {
    fn drop(&mut self) {
        if let Self::Ref(id) = self {
            panic!("Value::Ref({id:?}) dropped without calling drop_with() - this is a reference counting bug");
        }
    }
}

impl From<bool> for Value {
    fn from(v: bool) -> Self {
        Self::Bool(v)
    }
}

impl<'h> PyTrait<'h> for Value {
    fn py_type(&self, vm: &VM<'_>) -> Type {
        match self {
            Self::Undefined => panic!("Cannot get type of undefined value"),
            Self::Ellipsis => Type::Ellipsis,
            Self::NotImplemented => Type::NotImplementedType,
            Self::None => Type::NoneType,
            Self::Bool(_) => Type::Bool,
            Self::Int(_) | Self::InternLongInt(_) => Type::Int,
            Self::Float(_) => Type::Float,
            Self::InternString(_) => Type::Str,
            Self::InternBytes(_) => Type::Bytes,
            Self::Builtin(c) => c.py_type(),
            Self::ModuleFunction(_) => Type::BuiltinFunction,
            Self::DefFunction(_) => Type::Function,
            Self::Marker(m) => m.py_type(),
            Self::Property(_) => Type::Property,
            Self::Ref(id) => vm.heap.read(*id).py_type(vm),
            #[cfg(feature = "memory-model-checks")]
            Self::Dereferenced => panic!("Cannot access Dereferenced object"),
        }
    }

    fn py_len(&self, vm: &VM<'_>) -> Option<usize> {
        match self {
            // Count Unicode characters, not bytes, to match Python semantics
            Self::InternString(string_id) => Some(vm.interns.get_str(*string_id).chars().count()),
            Self::InternBytes(bytes_id) => Some(vm.interns.get_bytes(*bytes_id).len()),
            Self::Ref(id) => vm.heap.read(*id).py_len(vm),
            _ => None,
        }
    }

    fn py_eq_impl(&self, other: &Value, vm: &mut VM<'_>) -> RunResult<Option<bool>> {
        match self {
            // `Undefined` is a sentinel and is never equal to anything.
            Self::Undefined => Ok(Some(false)),

            Self::None => Ok(matches!(other, Self::None).then_some(true)),
            Self::Ellipsis => Ok(matches!(other, Self::Ellipsis).then_some(true)),
            Self::NotImplemented => Ok(matches!(other, Self::NotImplemented).then_some(true)),
            Self::Bool(b) => Ok(eq_i64(i64::from(*b), other, vm)),
            Self::Int(a) => Ok(eq_i64(*a, other, vm)),
            Self::Float(f) => Ok(eq_f64(*f, other, vm)),
            // `InternLongInt` is normally materialised to a heap `LongInt` before
            // it can be compared, but handle it directly so equality never
            // silently diverges if one reaches here.
            Self::InternLongInt(id) => Ok(eq_bigint(vm.interns.get_long_int(*id), other, vm)),
            Self::InternString(id) => Ok(match other {
                // Interned strings are deduplicated, so equal ids ⇔ equal content.
                Self::InternString(o) => Some(id == o),
                _ => eq_str(vm.interns.get_str(*id), other, vm),
            }),
            Self::InternBytes(id) => Ok(match other {
                // Fast path for the same interned bytes; otherwise compare content
                // (interned bytes are not deduplicated, unlike strings).
                Self::InternBytes(o) if id == o => Some(true),
                _ => eq_bytes(vm.interns.get_bytes(*id), other, vm),
            }),
            Self::Builtin(b) => Ok(match other {
                Self::Builtin(o) => Some(b == o),
                _ => None,
            }),
            Self::ModuleFunction(mf) => Ok(match other {
                Self::ModuleFunction(o) => Some(mf == o),
                _ => None,
            }),
            Self::DefFunction(f) => Ok(match other {
                Self::DefFunction(o) => Some(f == o),
                _ => None,
            }),
            Self::Marker(m) => Ok(match other {
                Self::Marker(o) => Some(m == o),
                _ => None,
            }),
            Self::Property(p) => Ok(match other {
                Self::Property(o) => Some(p == o),
                _ => None,
            }),
            Self::Ref(id) => vm.heap.read(*id).py_eq_impl(other, vm),
            #[cfg(feature = "memory-model-checks")]
            Self::Dereferenced => panic!("Cannot access Dereferenced object"),
        }
    }

    fn py_cmp(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<CmpOrder> {
        let interns = vm.interns;
        // py_cmp handles numbers, strings, bytes, tuples, and lists.
        // Recursion depth tracking for tuples/lists is handled by their iterators.
        //
        // `from_numeric` maps a `None` from a numeric helper to
        // `CmpOrder::Unordered` (only `NaN` yields `None` there), while
        // `from_total` maps `None` from a total-order comparison to
        // `CmpOrder::Incomparable`. Any operand pair with no comparison at all
        // falls through to the `Incomparable` catch-alls below.
        match (self, other) {
            (Self::Int(s), Self::Int(o)) => Ok(CmpOrder::Ordered(s.cmp(o))),
            (Self::Float(s), Self::Float(o)) => Ok(CmpOrder::from_numeric(s.partial_cmp(o))),
            // Int/float ordering is exact (no rounding of either operand).
            (Self::Int(s), Self::Float(o)) => Ok(CmpOrder::from_numeric(i64_cmp_f64(*s, *o))),
            (Self::Float(s), Self::Int(o)) => Ok(CmpOrder::from_numeric(i64_cmp_f64(*o, *s).map(Ordering::reverse))),
            (Self::Int(a), Self::InternLongInt(b)) => Ok(CmpOrder::Ordered(
                bigint_cmp_i64(interns.get_long_int(*b), *a).reverse(),
            )),
            (Self::InternLongInt(a), Self::Int(b)) => {
                Ok(CmpOrder::Ordered(bigint_cmp_i64(interns.get_long_int(*a), *b)))
            }
            (Self::Float(a), Self::InternLongInt(b)) => Ok(CmpOrder::from_numeric(
                bigint_cmp_f64(interns.get_long_int(*b), *a).map(Ordering::reverse),
            )),
            (Self::InternLongInt(a), Self::Float(b)) => {
                Ok(CmpOrder::from_numeric(bigint_cmp_f64(interns.get_long_int(*a), *b)))
            }
            (Self::InternLongInt(a), Self::InternLongInt(b)) => Ok(CmpOrder::Ordered(
                interns.get_long_int(*a).cmp(interns.get_long_int(*b)),
            )),
            // Bool promotion: convert to Int and re-dispatch. Recursion is bounded
            // to at most 2 levels (Bool→Int, then Int matches directly above).
            (Self::Bool(s), _) => Self::Int(i64::from(*s)).py_cmp(other, vm),
            (_, Self::Bool(s)) => self.py_cmp(&Self::Int(i64::from(*s)), vm),
            // Int vs LongInt comparison
            (Self::Int(a), Self::Ref(id)) if let HeapData::LongInt(li) = vm.heap.get(*id) => {
                Ok(CmpOrder::Ordered(bigint_cmp_i64(li.inner(), *a).reverse()))
            }
            (Self::InternLongInt(a), Self::Ref(id)) if let HeapData::LongInt(li) = vm.heap.get(*id) => {
                Ok(CmpOrder::Ordered(interns.get_long_int(*a).cmp(li.inner())))
            }
            // LongInt vs Int comparison
            (Self::Ref(id), Self::Int(b)) if let HeapData::LongInt(li) = vm.heap.get(*id) => {
                Ok(CmpOrder::Ordered(bigint_cmp_i64(li.inner(), *b)))
            }
            (Self::Ref(id), Self::InternLongInt(b)) if let HeapData::LongInt(li) = vm.heap.get(*id) => {
                Ok(CmpOrder::Ordered(li.inner().cmp(interns.get_long_int(*b))))
            }
            // Float vs LongInt comparison (exact, no precision loss)
            (Self::Float(s), Self::Ref(id)) if let HeapData::LongInt(li) = vm.heap.get(*id) => Ok(
                CmpOrder::from_numeric(bigint_cmp_f64(li.inner(), *s).map(Ordering::reverse)),
            ),
            // LongInt vs Float comparison (exact, no precision loss)
            (Self::Ref(id), Self::Float(o)) if let HeapData::LongInt(li) = vm.heap.get(*id) => {
                Ok(CmpOrder::from_numeric(li.partial_cmp_f64(*o)))
            }
            // Ref vs Ref comparison: handles LongInt, Str, Tuple, and List
            (Self::Ref(id1), Self::Ref(id2)) => match (vm.heap.read(*id1), vm.heap.read(*id2)) {
                (HeapReadOutput::LongInt(a), HeapReadOutput::LongInt(b)) => {
                    Ok(CmpOrder::Ordered(a.get(vm.heap).inner().cmp(b.get(vm.heap).inner())))
                }
                (HeapReadOutput::Str(a), HeapReadOutput::Str(b)) => {
                    Ok(CmpOrder::Ordered(a.get(vm.heap).as_str().cmp(b.get(vm.heap).as_str())))
                }
                (HeapReadOutput::Tuple(a), HeapReadOutput::Tuple(b)) => a.py_cmp(&b, vm),
                // A namedtuple orders like the tuple it subclasses, including
                // against a plain tuple in either direction. Both sides are
                // cloned first because the comparison needs `&mut VM`, which
                // cannot coexist with two live `HeapRead`s.
                (HeapReadOutput::NamedTuple(a), HeapReadOutput::NamedTuple(b)) => {
                    let (a, b) = (a.cloned_items(vm)?, b.cloned_items(vm)?);
                    cmp_item_seqs(a, b, vm)
                }
                (HeapReadOutput::NamedTuple(a), HeapReadOutput::Tuple(b)) => {
                    let (a, b) = (a.cloned_items(vm)?, b.cloned_items(vm)?);
                    cmp_item_seqs(a, b, vm)
                }
                (HeapReadOutput::Tuple(a), HeapReadOutput::NamedTuple(b)) => {
                    let (a, b) = (a.cloned_items(vm)?, b.cloned_items(vm)?);
                    cmp_item_seqs(a, b, vm)
                }
                (HeapReadOutput::List(a), HeapReadOutput::List(b)) => a.py_cmp(&b, vm),
                (HeapReadOutput::Deque(a), HeapReadOutput::Deque(b)) => a.py_cmp(&b, vm),
                (HeapReadOutput::Date(a), HeapReadOutput::Date(b)) => {
                    Ok(CmpOrder::from_total(a.get(vm.heap).partial_cmp(b.get(vm.heap))))
                }
                (HeapReadOutput::DateTime(a), HeapReadOutput::DateTime(b)) => a.py_cmp(&b, vm),
                (HeapReadOutput::TimeDelta(a), HeapReadOutput::TimeDelta(b)) => {
                    Ok(CmpOrder::from_total(a.get(vm.heap).partial_cmp(b.get(vm.heap))))
                }
                _ => Ok(CmpOrder::Incomparable),
            },
            // Interned string comparisons
            (Self::InternString(s1), Self::InternString(s2)) => {
                Ok(CmpOrder::Ordered(interns.get_str(*s1).cmp(interns.get_str(*s2))))
            }
            // Cross-type string comparisons: interned vs heap-allocated
            (Self::InternString(s1), Self::Ref(id2)) if let HeapData::Str(s2) = vm.heap.get(*id2) => {
                Ok(CmpOrder::Ordered(interns.get_str(*s1).cmp(s2.as_str())))
            }
            (Self::Ref(id1), Self::InternString(s2)) if let HeapData::Str(s1) = vm.heap.get(*id1) => {
                Ok(CmpOrder::Ordered(s1.as_str().cmp(interns.get_str(*s2))))
            }
            (Self::InternBytes(b1), Self::InternBytes(b2)) => {
                Ok(CmpOrder::Ordered(interns.get_bytes(*b1).cmp(interns.get_bytes(*b2))))
            }
            _ => Ok(CmpOrder::Incomparable),
        }
    }

    fn py_bool(&self, vm: &mut VM<'_>) -> RunResult<bool> {
        match self {
            Self::NotImplemented => Err(SimpleException::new_msg(
                ExcType::TypeError,
                "NotImplemented should not be used in a boolean context",
            )
            .into()),
            Self::Ref(id) => vm.heap.read(*id).py_bool(vm),
            Self::Undefined | Self::None => Ok(false),
            Self::Ellipsis => Ok(true),
            Self::Bool(b) => Ok(*b),
            Self::Int(v) => Ok(*v != 0),
            Self::Float(f) => Ok(*f != 0.0),
            // InternLongInt is always truthy (if it were zero, it would fit in i64).
            Self::InternLongInt(_) => Ok(true),
            Self::Builtin(_) | Self::ModuleFunction(_) => Ok(true),
            Self::DefFunction(_) => Ok(true),
            Self::Marker(_) | Self::Property(_) => Ok(true),
            Self::InternString(string_id) => Ok(!vm.interns.get_str(*string_id).is_empty()),
            Self::InternBytes(bytes_id) => Ok(!vm.interns.get_bytes(*bytes_id).is_empty()),
            #[cfg(feature = "memory-model-checks")]
            Self::Dereferenced => panic!("Cannot access Dereferenced object"),
        }
    }

    fn py_repr_fmt(&self, f: &mut impl Write, vm: &mut VM<'_>, heap_ids: &mut LazyHeapSet) -> RunResult<()> {
        let interns = vm.interns;
        match self {
            Self::Undefined => Ok(f.write_str("Undefined")?),
            Self::Ellipsis => Ok(f.write_str("Ellipsis")?),
            Self::NotImplemented => Ok(f.write_str("NotImplemented")?),
            Self::None => Ok(f.write_str("None")?),
            Self::Bool(true) => Ok(f.write_str("True")?),
            Self::Bool(false) => Ok(f.write_str("False")?),
            // `itoa` formats into a fixed stack buffer, skipping the generic
            // `fmt`/`pad_integral` path and its repeated `RawVec` reallocation.
            Self::Int(v) => Ok(f.write_str(itoa::Buffer::new().format(*v))?),
            Self::InternLongInt(long_int_id) => {
                let bi = interns.get_long_int(*long_int_id);
                check_bits_str_digits_limit(bi.bits())?;
                Ok(write!(f, "{bi}")?)
            }
            Self::Float(v) => Ok(write!(f, "{}", FormatFloat(*v))?),
            Self::Builtin(b) => Ok(b.py_repr_fmt(f)?),
            Self::ModuleFunction(mf) => {
                let py_id = self.id().into_value(vm.heap);
                defer_drop!(py_id, vm);
                Ok(mf.py_repr_fmt(f, PythonIdDisplay::new(py_id, vm.heap))?)
            }
            Self::DefFunction(f_id) => {
                let py_id = self.id().into_value(vm.heap);
                defer_drop!(py_id, vm);
                Ok(interns
                    .get_function(*f_id)
                    .py_repr_fmt(f, interns, PythonIdDisplay::new(py_id, vm.heap))?)
            }
            Self::InternString(string_id) => Ok(string_repr_fmt(interns.get_str(*string_id), f)?),
            Self::InternBytes(bytes_id) => Ok(bytes_repr_fmt(interns.get_bytes(*bytes_id), f)?),
            Self::Marker(m) => Ok(m.py_repr_fmt(f)?),
            Self::Property(p) => Ok(write!(f, "<property {p:?}>")?),
            Self::Ref(id) => {
                if heap_ids.contains(id) {
                    // Cycle detected - write type-specific placeholder following Python semantics
                    match vm.heap.get(*id) {
                        HeapData::List(_) => Ok(f.write_str("[...]")?),
                        HeapData::Tuple(_) => Ok(f.write_str("(...)")?),
                        HeapData::Dict(_) => Ok(f.write_str("{...}")?),
                        // A deque prints its items inside list-shaped brackets
                        // (`deque([...])`), so the placeholder is a list's.
                        HeapData::Deque(_) => Ok(f.write_str("[...]")?),
                        // Other types don't typically have cycles, but handle gracefully
                        _ => Ok(f.write_str("...")?),
                    }
                } else if matches!(vm.heap.get(*id), HeapData::Instance(_)) {
                    // Handled here, not at the heap level, because dispatch needs
                    // the heap id for `self`. A user `__repr__` recurses on the
                    // *Rust* stack (see limitations/classes.md); the synthesized
                    // dataclass form carries `heap_ids`, so a cycle in one hits
                    // the branch above.
                    instance_repr_fmt(*id, f, vm, heap_ids)
                } else {
                    heap_ids.insert(*id);
                    let result = vm.heap.read(*id).py_repr_fmt(f, vm, heap_ids);
                    heap_ids.remove(id);
                    result
                }
            }
            #[cfg(feature = "memory-model-checks")]
            Self::Dereferenced => panic!("Cannot access Dereferenced object"),
        }
    }

    /// Overrides the default `py_repr` with allocation-light fast paths for the
    /// values whose repr is cheap to produce:
    /// - singletons (`None`/`True`/`False`/`Ellipsis`) resolve to a pre-interned
    ///   `StringId`, so `repr`/`str`/`print`/f-strings allocate nothing at all;
    /// - `int` formats via `itoa` straight into a right-sized `allocate_string`,
    ///   skipping the grow-then-shrink intermediate `String`.
    ///
    /// Every other variant takes the generic `py_repr_fmt` buffered path. `str`
    /// is also served allocation-free, but by [`py_str`](Self::py_str) — `repr`
    /// of a `str` still needs a buffer for quoting/escaping.
    fn py_repr(&self, vm: &mut VM<'h>) -> RunResult<Value> {
        match self {
            Self::None => Ok(Self::InternString(StaticStrings::NoneRepr.into())),
            Self::Bool(true) => Ok(Self::InternString(StaticStrings::TrueRepr.into())),
            Self::Bool(false) => Ok(Self::InternString(StaticStrings::FalseRepr.into())),
            Self::Ellipsis => Ok(Self::InternString(StaticStrings::EllipsisRepr.into())),
            Self::NotImplemented => Ok(Self::InternString(StaticStrings::NotImplementedRepr.into())),
            Self::Int(i) => Ok(allocate_string(itoa::Buffer::new().format(*i), vm.heap)),
            _ => {
                let mut s = String::new();
                let mut heap_ids = LazyHeapSet::default();
                self.py_repr_fmt(&mut s, vm, &mut heap_ids)?;
                Ok(allocate_string(s, vm.heap))
            }
        }
    }

    fn py_str(&self, vm: &mut VM<'h>) -> RunResult<Value> {
        match self {
            // Interned/heap strings are already what `str()` returns — hand the
            // same value back (inc-ref'd for the heap case) instead of cloning
            // the bytes into a fresh allocation.
            Self::InternString(string_id) => Ok(Self::InternString(*string_id)),
            Self::Ref(id) if matches!(vm.heap.get(*id), HeapData::Str(_)) => Ok(self.clone_with_heap(vm.heap)),
            // Instances dispatch to a user `__str__`/`__repr__` (needs the heap id).
            Self::Ref(id) if matches!(vm.heap.get(*id), HeapData::Instance(_)) => instance_str(*id, vm),
            Self::Ref(id) => vm.heap.read(*id).py_str(vm),
            _ => self.py_repr(vm),
        }
    }

    fn py_iadd_impl(&mut self, other: &Self, vm: &mut VM<'_>, _self_id: Option<HeapId>) -> RunResult<bool> {
        if let Self::Ref(id) = self {
            vm.heap.read(*id).py_iadd_impl(other, vm, Some(*id))
        } else {
            Ok(false)
        }
    }

    fn py_isub_impl(&mut self, other: &Self, vm: &mut VM<'_>, _self_id: Option<HeapId>) -> RunResult<bool> {
        if let Self::Ref(id) = self {
            vm.heap.read(*id).py_isub_impl(other, vm, Some(*id))
        } else {
            Ok(false)
        }
    }

    fn py_iand_impl(&mut self, other: &Self, vm: &mut VM<'_>, _self_id: Option<HeapId>) -> RunResult<bool> {
        if let Self::Ref(id) = self {
            vm.heap.read(*id).py_iand_impl(other, vm, Some(*id))
        } else {
            Ok(false)
        }
    }

    fn py_ior_impl(&mut self, other: &Self, vm: &mut VM<'_>, _self_id: Option<HeapId>) -> RunResult<bool> {
        if let Self::Ref(id) = self {
            vm.heap.read(*id).py_ior_impl(other, vm, Some(*id))
        } else {
            Ok(false)
        }
    }

    fn py_cmp_op(
        &self,
        other: &Self,
        op: CmpOperator,
        vm: &mut VM<'_>,
        _self_id: Option<HeapId>,
    ) -> RunResult<Option<bool>> {
        if let Self::Ref(id) = self {
            vm.heap.read(*id).py_cmp_op(other, op, vm, Some(*id))
        } else {
            Ok(None)
        }
    }

    fn py_neg_impl(&self, vm: &mut VM<'_>, _self_id: Option<HeapId>) -> RunResult<Option<Self>> {
        match self {
            // `checked_neg` catches `i64::MIN`, whose negation only fits a LongInt.
            Self::Int(n) => match n.checked_neg() {
                Some(negated) => Ok(Some(Self::Int(negated))),
                None => Ok(Some((-LongInt::from(*n)).into_value(vm.heap))),
            },
            Self::Float(f) => Ok(Some(Self::Float(-f))),
            Self::Bool(b) => Ok(Some(Self::Int(if *b { -1 } else { 0 }))),
            Self::Ref(id) => vm.heap.read(*id).py_neg_impl(vm, Some(*id)),
            _ => Ok(None),
        }
    }

    fn py_pos_impl(&self, vm: &mut VM<'_>, _self_id: Option<HeapId>) -> RunResult<Option<Self>> {
        match self {
            // `+x` leaves a number as it is; the clone is what the caller pushes
            // in place of the operand it drops.
            Self::Int(_) | Self::Float(_) => Ok(Some(self.clone_with_heap(vm.heap))),
            Self::Bool(b) => Ok(Some(Self::Int(i64::from(*b)))),
            Self::Ref(id) => vm.heap.read(*id).py_pos_impl(vm, Some(*id)),
            _ => Ok(None),
        }
    }

    /// One-sided implementation of Python `+`.
    fn py_add_impl(&self, other: &Self, vm: &mut VM<'_>, _self_id: Option<HeapId>) -> RunResult<Option<Self>> {
        let interns = vm.interns;
        match (self, other) {
            // Int + Int with overflow detection
            (Self::Int(a), Self::Int(b)) => {
                if let Some(result) = a.checked_add(*b) {
                    Ok(Some(Self::Int(result)))
                } else {
                    Ok(Some(wide_i128_into_value(i128::from(*a) + i128::from(*b), vm.heap)))
                }
            }
            (Self::Float(v1), Self::Float(v2)) => Ok(Some(Self::Float(v1 + v2))),
            // Int + Float and Float + Int
            (Self::Int(a), Self::Float(b)) => Ok(Some(Self::Float(*a as f64 + b))),
            (Self::Float(a), Self::Int(b)) => Ok(Some(Self::Float(a + *b as f64))),
            (Self::InternString(s1), Self::InternString(s2)) => Ok(Some(concat_allocate_str(
                interns.get_str(*s1),
                interns.get_str(*s2),
                vm.heap,
            )?)),
            // for strings we need to account for the fact they might be either interned or not
            (Self::InternString(string_id), Self::Ref(id2)) if let HeapData::Str(s2) = vm.heap.get(*id2) => Ok(Some(
                concat_allocate_str(interns.get_str(*string_id), s2.as_str(), vm.heap)?,
            )),
            // same for bytes
            (Self::InternBytes(lhs), Self::InternBytes(rhs)) => Ok(Some(concat_bytes(
                interns.get_bytes(*lhs),
                interns.get_bytes(*rhs),
                vm.heap,
            )?)),
            (Self::InternBytes(lhs), Self::Ref(rhs)) if let HeapData::Bytes(rhs) = vm.heap.get(*rhs) => {
                Ok(Some(concat_bytes(interns.get_bytes(*lhs), rhs.as_slice(), vm.heap)?))
            }
            (Self::Ref(id), _) => vm.heap.read(*id).py_add_impl(other, vm, Some(*id)),
            _ => Ok(None),
        }
    }

    /// Reflected implementation of Python `+`.
    fn py_radd_impl(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        if let Self::Ref(id) = self {
            vm.heap.read(*id).py_radd_impl(other, vm)
        } else {
            Ok(None)
        }
    }

    /// One-sided implementation of Python `-`.
    fn py_sub_impl(&self, other: &Self, vm: &mut VM<'_>, _self_id: Option<HeapId>) -> RunResult<Option<Self>> {
        match (self, other) {
            // Int - Int with overflow detection
            (Self::Int(a), Self::Int(b)) => {
                if let Some(result) = a.checked_sub(*b) {
                    Ok(Some(Self::Int(result)))
                } else {
                    Ok(Some(wide_i128_into_value(i128::from(*a) - i128::from(*b), vm.heap)))
                }
            }
            // Float - Float
            (Self::Float(a), Self::Float(b)) => Ok(Some(Self::Float(a - b))),
            // Int - Float and Float - Int
            (Self::Int(a), Self::Float(b)) => Ok(Some(Self::Float(*a as f64 - b))),
            (Self::Float(a), Self::Int(b)) => Ok(Some(Self::Float(a - *b as f64))),
            (Self::Ref(id), _) => vm.heap.read(*id).py_sub_impl(other, vm, Some(*id)),
            _ => Ok(None),
        }
    }

    /// Reflected implementation of Python `-`.
    fn py_rsub_impl(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        if let Self::Ref(id) = self {
            vm.heap.read(*id).py_rsub_impl(other, vm)
        } else {
            Ok(None)
        }
    }

    /// One-sided implementation of Python `*`.
    fn py_mul_impl(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        match (self, other) {
            (Self::Int(a), Self::Int(b)) => {
                if let Some(result) = a.checked_mul(*b) {
                    Ok(Some(Self::Int(result)))
                } else {
                    Ok(Some(wide_i128_into_value(i128::from(*a) * i128::from(*b), vm.heap)))
                }
            }
            (Self::Float(a), Self::Float(b)) => Ok(Some(Self::Float(a * b))),
            (Self::Int(a), Self::Float(b)) => Ok(Some(Self::Float(*a as f64 * b))),
            (Self::Float(a), Self::Int(b)) => Ok(Some(Self::Float(a * *b as f64))),
            (Self::Bool(a), Self::Int(b)) => Ok(Some(Self::Int(i64::from(*a) * b))),
            (Self::Int(a), Self::Bool(b)) => Ok(Some(Self::Int(a * i64::from(*b)))),
            (Self::Bool(a), Self::Float(b)) => Ok(Some(Self::Float(f64::from(*a) * b))),
            (Self::Float(a), Self::Bool(b)) => Ok(Some(Self::Float(a * f64::from(*b)))),
            (Self::Bool(a), Self::Bool(b)) => Ok(Some(Self::Int(i64::from(*a) * i64::from(*b)))),
            (Self::InternString(id), count) | (count, Self::InternString(id)) => {
                let Some(count) = repeat_count(count, vm)? else {
                    return Ok(None);
                };
                Ok(Some(repeat_str(vm.interns.get_str(*id), count, vm.heap)?))
            }
            (Self::InternBytes(id), count) | (count, Self::InternBytes(id)) => {
                let Some(count) = repeat_count(count, vm)? else {
                    return Ok(None);
                };
                Ok(Some(repeat_bytes(vm.interns.get_bytes(*id), count, vm.heap)?))
            }
            (Self::Ref(id), _) => vm.heap.read(*id).py_mul_impl(other, vm),
            _ => Ok(None),
        }
    }

    /// Reflected implementation of Python `*`.
    fn py_rmul_impl(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        if let Self::Ref(id) = self {
            vm.heap.read(*id).py_rmul_impl(other, vm)
        } else {
            Ok(None)
        }
    }

    /// One-sided implementation of Python `/`.
    fn py_truediv_impl(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        match (self, other) {
            // True division always returns float
            (Self::Int(a), Self::Int(b)) => {
                if *b == 0 {
                    Err(ExcType::zero_division().into())
                } else {
                    Ok(Some(Self::Float(*a as f64 / *b as f64)))
                }
            }
            (Self::Float(a), Self::Float(b)) => {
                if *b == 0.0 {
                    Err(ExcType::zero_division().into())
                } else {
                    Ok(Some(Self::Float(a / b)))
                }
            }
            (Self::Int(a), Self::Float(b)) => {
                if *b == 0.0 {
                    Err(ExcType::zero_division().into())
                } else {
                    Ok(Some(Self::Float(*a as f64 / b)))
                }
            }
            (Self::Float(a), Self::Int(b)) => {
                if *b == 0 {
                    Err(ExcType::zero_division().into())
                } else {
                    Ok(Some(Self::Float(a / *b as f64)))
                }
            }
            // Bool division (True=1, False=0)
            (Self::Bool(a), Self::Int(b)) => {
                if *b == 0 {
                    Err(ExcType::zero_division().into())
                } else {
                    Ok(Some(Self::Float(f64::from(*a) / *b as f64)))
                }
            }
            (Self::Int(a), Self::Bool(b)) => {
                if *b {
                    Ok(Some(Self::Float(*a as f64))) // a / 1 = a
                } else {
                    Err(ExcType::zero_division().into())
                }
            }
            (Self::Bool(a), Self::Float(b)) => {
                if *b == 0.0 {
                    Err(ExcType::zero_division().into())
                } else {
                    Ok(Some(Self::Float(f64::from(*a) / b)))
                }
            }
            (Self::Float(a), Self::Bool(b)) => {
                if *b {
                    Ok(Some(Self::Float(*a))) // a / 1.0 = a
                } else {
                    Err(ExcType::zero_division().into())
                }
            }
            (Self::Bool(a), Self::Bool(b)) => {
                if *b {
                    Ok(Some(Self::Float(f64::from(*a)))) // a / 1 = a
                } else {
                    Err(ExcType::zero_division().into())
                }
            }
            (Self::Ref(id), _) => vm.heap.read(*id).py_truediv_impl(other, vm),
            _ => Ok(None),
        }
    }

    /// Reflected implementation of Python `/`.
    fn py_rtruediv_impl(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        if let Self::Ref(id) = self {
            vm.heap.read(*id).py_rtruediv_impl(other, vm)
        } else {
            Ok(None)
        }
    }

    /// One-sided implementation of Python `//`.
    fn py_floordiv_impl(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        match (self, other) {
            // Floor division: int // int returns int
            (Self::Int(a), Self::Int(b)) => {
                if *b == 0 {
                    Err(ExcType::zero_division().into())
                } else if let Some((d, _)) = floor_divmod(*a, *b) {
                    Ok(Some(Self::Int(d)))
                } else {
                    Ok(Some(wide_i128_into_value(
                        i128::from(*a).div_euclid(i128::from(*b)),
                        vm.heap,
                    )))
                }
            }
            // Float floor division returns float
            (Self::Float(a), Self::Float(b)) => {
                if *b == 0.0 {
                    Err(ExcType::zero_division().into())
                } else {
                    Ok(Some(Self::Float((a / b).floor())))
                }
            }
            (Self::Int(a), Self::Float(b)) => {
                if *b == 0.0 {
                    Err(ExcType::zero_division().into())
                } else {
                    Ok(Some(Self::Float((*a as f64 / b).floor())))
                }
            }
            (Self::Float(a), Self::Int(b)) => {
                if *b == 0 {
                    Err(ExcType::zero_division().into())
                } else {
                    Ok(Some(Self::Float((a / *b as f64).floor())))
                }
            }
            // Bool floor division (True=1, False=0)
            (Self::Bool(a), Self::Int(b)) => {
                if *b == 0 {
                    Err(ExcType::zero_division().into())
                } else {
                    let a_int = i64::from(*a);
                    // Use same floor division logic as Int // Int
                    let d = a_int / b;
                    let r = a_int % b;
                    let result = if r != 0 && (a_int < 0) != (*b < 0) { d - 1 } else { d };
                    Ok(Some(Self::Int(result)))
                }
            }
            (Self::Int(a), Self::Bool(b)) => {
                if *b {
                    Ok(Some(Self::Int(*a))) // a // 1 = a
                } else {
                    Err(ExcType::zero_division().into())
                }
            }
            (Self::Bool(a), Self::Float(b)) => {
                if *b == 0.0 {
                    Err(ExcType::zero_division().into())
                } else {
                    Ok(Some(Self::Float((f64::from(*a) / b).floor())))
                }
            }
            (Self::Float(a), Self::Bool(b)) => {
                if *b {
                    Ok(Some(Self::Float(a.floor()))) // a // 1.0 = floor(a)
                } else {
                    Err(ExcType::zero_division().into())
                }
            }
            (Self::Bool(a), Self::Bool(b)) => {
                if *b {
                    Ok(Some(Self::Int(i64::from(*a)))) // a // 1 = a
                } else {
                    Err(ExcType::zero_division().into())
                }
            }
            (Self::Ref(id), _) => vm.heap.read(*id).py_floordiv_impl(other, vm),
            _ => Ok(None),
        }
    }

    /// Reflected implementation of Python `//`.
    fn py_rfloordiv_impl(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        if let Self::Ref(id) = self {
            vm.heap.read(*id).py_rfloordiv_impl(other, vm)
        } else {
            Ok(None)
        }
    }

    /// One-sided implementation of Python `%`.
    fn py_mod_impl(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        match (self, other) {
            (Self::Int(a), Self::Int(b)) => {
                if *b == 0 {
                    Err(ExcType::zero_division().into())
                } else if let Some(r) = a.checked_rem(*b) {
                    // Python modulo: result has the same sign as divisor (b)
                    let result = if r != 0 && (*a < 0) != (*b < 0) { r + *b } else { r };
                    Ok(Some(Self::Int(result)))
                } else {
                    // Overflow - i64::MIN % -1 is 0
                    Ok(Some(Self::Int(0)))
                }
            }
            (Self::Float(v1), Self::Float(v2)) => {
                if *v2 == 0.0 {
                    Err(ExcType::zero_division().into())
                } else {
                    Ok(Some(Self::Float(py_float_mod(*v1, *v2))))
                }
            }
            (Self::Float(v1), Self::Int(v2)) => {
                if *v2 == 0 {
                    Err(ExcType::zero_division().into())
                } else {
                    Ok(Some(Self::Float(py_float_mod(*v1, *v2 as f64))))
                }
            }
            (Self::Int(v1), Self::Float(v2)) => {
                if *v2 == 0.0 {
                    Err(ExcType::zero_division().into())
                } else {
                    Ok(Some(Self::Float(py_float_mod(*v1 as f64, *v2))))
                }
            }
            (Self::Ref(id), _) => vm.heap.read(*id).py_mod_impl(other, vm),
            _ => Ok(None),
        }
    }

    /// Reflected implementation of Python `%`.
    fn py_rmod_impl(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        if let Self::Ref(id) = self {
            vm.heap.read(*id).py_rmod_impl(other, vm)
        } else {
            Ok(None)
        }
    }

    /// One-sided implementation of Python `** or pow()`.
    fn py_pow_impl(&self, other: &Self, modulus: Option<&Self>, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        if modulus.is_some() {
            if let Self::Ref(id) = self {
                vm.heap.read(*id).py_pow_impl(other, modulus, vm)
            } else {
                Ok(None)
            }
        } else {
            match (self, other) {
                (Self::Int(base), Self::Int(exp)) => {
                    if *base == 0 && *exp < 0 {
                        Err(ExcType::zero_negative_power())
                    } else if *exp >= 0 {
                        // Positive exponent: try to return int, promote to LongInt on overflow
                        if let Ok(exp_u32) = u32::try_from(*exp) {
                            if let Some(result) = base.checked_pow(exp_u32) {
                                Ok(Some(Self::Int(result)))
                            } else if let Some(result) = i128::from(*base).checked_pow(exp_u32) {
                                Ok(Some(wide_i128_into_value(result, vm.heap)))
                            } else {
                                check_pow_size(i64_bits(*base), u64::from(exp_u32), vm.heap.tracker())?;
                                let bi = BigInt::from(*base).pow(exp_u32);
                                Ok(Some(LongInt::new(bi).into_value(vm.heap)))
                            }
                        } else {
                            // exp > u32::MAX - use BigInt with modpow-style exponentiation
                            // For very large exponents, we still need LongInt
                            // Safety: exp >= 0 is guaranteed by the outer if condition
                            #[expect(clippy::cast_sign_loss)]
                            let exp_u64 = *exp as u64;
                            // Check size before computing to prevent DoS
                            check_pow_size(i64_bits(*base), exp_u64, vm.heap.tracker())?;
                            let bi = bigint_pow(BigInt::from(*base), exp_u64);
                            Ok(Some(LongInt::new(bi).into_value(vm.heap)))
                        }
                    } else {
                        // Negative exponent: return float
                        // Use powi if exp fits in i32, otherwise use powf
                        if let Ok(exp_i32) = i32::try_from(*exp) {
                            Ok(Some(Self::Float((*base as f64).powi(exp_i32))))
                        } else {
                            Ok(Some(Self::Float((*base as f64).powf(*exp as f64))))
                        }
                    }
                }
                (Self::Float(base), Self::Float(exp)) => {
                    if *base == 0.0 && *exp < 0.0 {
                        Err(ExcType::zero_negative_power())
                    } else {
                        Ok(Some(Self::Float(base.powf(*exp))))
                    }
                }
                (Self::Int(base), Self::Float(exp)) => {
                    if *base == 0 && *exp < 0.0 {
                        Err(ExcType::zero_negative_power())
                    } else {
                        Ok(Some(Self::Float((*base as f64).powf(*exp))))
                    }
                }
                (Self::Float(base), Self::Int(exp)) => {
                    if *base == 0.0 && *exp < 0 {
                        Err(ExcType::zero_negative_power())
                    } else if let Ok(exp_i32) = i32::try_from(*exp) {
                        // Use powi if exp fits in i32
                        Ok(Some(Self::Float(base.powi(exp_i32))))
                    } else {
                        // Fall back to powf for exponents outside i32 range
                        Ok(Some(Self::Float(base.powf(*exp as f64))))
                    }
                }
                // Bool power operations (True=1, False=0)
                (Self::Bool(base), Self::Int(exp)) => {
                    let base_int = i64::from(*base);
                    if base_int == 0 && *exp < 0 {
                        Err(ExcType::zero_negative_power())
                    } else if *exp >= 0 {
                        // Positive exponent: 1**n=1, 0**n=0 (for n>0), 0**0=1
                        if let Ok(exp_u32) = u32::try_from(*exp) {
                            match base_int.checked_pow(exp_u32) {
                                Some(result) => Ok(Some(Self::Int(result))),
                                None => Ok(Some(Self::Float((base_int as f64).powf(*exp as f64)))),
                            }
                        } else {
                            Ok(Some(Self::Float((base_int as f64).powf(*exp as f64))))
                        }
                    } else {
                        // Negative exponent: return float (1**-n=1.0)
                        if let Ok(exp_i32) = i32::try_from(*exp) {
                            Ok(Some(Self::Float((base_int as f64).powi(exp_i32))))
                        } else {
                            Ok(Some(Self::Float((base_int as f64).powf(*exp as f64))))
                        }
                    }
                }
                (Self::Int(base), Self::Bool(exp)) => {
                    // n ** True = n, n ** False = 1
                    if *exp {
                        Ok(Some(Self::Int(*base)))
                    } else {
                        Ok(Some(Self::Int(1)))
                    }
                }
                (Self::Bool(base), Self::Float(exp)) => {
                    let base_float = f64::from(*base);
                    if base_float == 0.0 && *exp < 0.0 {
                        Err(ExcType::zero_negative_power())
                    } else {
                        Ok(Some(Self::Float(base_float.powf(*exp))))
                    }
                }
                (Self::Float(base), Self::Bool(exp)) => {
                    // base ** True = base, base ** False = 1.0
                    if *exp {
                        Ok(Some(Self::Float(*base)))
                    } else {
                        Ok(Some(Self::Float(1.0)))
                    }
                }
                (Self::Bool(base), Self::Bool(exp)) => {
                    // True ** True = 1, True ** False = 1, False ** True = 0, False ** False = 1
                    let base_int = i64::from(*base);
                    let exp_int = i64::from(*exp);
                    if exp_int == 0 {
                        Ok(Some(Self::Int(1))) // anything ** 0 = 1
                    } else {
                        Ok(Some(Self::Int(base_int))) // base ** 1 = base
                    }
                }
                (Self::Ref(id), _) => vm.heap.read(*id).py_pow_impl(other, modulus, vm),
                _ => Ok(None),
            }
        }
    }

    /// Reflected implementation of Python `** or pow()`.
    fn py_rpow_impl(&self, other: &Self, modulus: Option<&Self>, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        if let Self::Ref(id) = self {
            vm.heap.read(*id).py_rpow_impl(other, modulus, vm)
        } else {
            Ok(None)
        }
    }

    /// One-sided implementation of Python `&`.
    fn py_and_impl(&self, other: &Self, vm: &mut VM<'_>, _self_id: Option<HeapId>) -> RunResult<Option<Self>> {
        if let (Self::Bool(lhs), Self::Bool(rhs)) = (self, other) {
            Ok(Some(Self::Bool(*lhs && *rhs)))
        } else if let (Some(lhs), Some(rhs)) = (immediate_int(self), immediate_int(other)) {
            Ok(Some(Self::Int(lhs & rhs)))
        } else if let Self::Ref(id) = self {
            vm.heap.read(*id).py_and_impl(other, vm, Some(*id))
        } else {
            Ok(None)
        }
    }

    /// Reflected implementation of Python `&`.
    fn py_rand_impl(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        if let Self::Ref(id) = self {
            vm.heap.read(*id).py_rand_impl(other, vm)
        } else {
            Ok(None)
        }
    }

    /// One-sided implementation of Python `|`.
    fn py_or_impl(&self, other: &Self, vm: &mut VM<'_>, _self_id: Option<HeapId>) -> RunResult<Option<Self>> {
        if let (Self::Bool(lhs), Self::Bool(rhs)) = (self, other) {
            Ok(Some(Self::Bool(*lhs || *rhs)))
        } else if let (Some(lhs), Some(rhs)) = (immediate_int(self), immediate_int(other)) {
            Ok(Some(Self::Int(lhs | rhs)))
        } else if let Self::Ref(id) = self {
            vm.heap.read(*id).py_or_impl(other, vm, Some(*id))
        } else {
            Ok(None)
        }
    }

    /// Reflected implementation of Python `|`.
    fn py_ror_impl(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        if let Self::Ref(id) = self {
            vm.heap.read(*id).py_ror_impl(other, vm)
        } else {
            Ok(None)
        }
    }

    /// One-sided implementation of Python `^`.
    fn py_xor_impl(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        if let (Self::Bool(lhs), Self::Bool(rhs)) = (self, other) {
            Ok(Some(Self::Bool(*lhs ^ *rhs)))
        } else if let (Some(lhs), Some(rhs)) = (immediate_int(self), immediate_int(other)) {
            Ok(Some(Self::Int(lhs ^ rhs)))
        } else if let Self::Ref(id) = self {
            vm.heap.read(*id).py_xor_impl(other, vm)
        } else {
            Ok(None)
        }
    }

    /// Reflected implementation of Python `^`.
    fn py_rxor_impl(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        if let Self::Ref(id) = self {
            vm.heap.read(*id).py_rxor_impl(other, vm)
        } else {
            Ok(None)
        }
    }

    /// One-sided implementation of Python `<<`.
    fn py_lshift_impl(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        if let (Some(lhs), Some(rhs)) = (immediate_int(self), immediate_int(other)) {
            if rhs < 0 {
                Err(ExcType::value_error_negative_shift_count())
            } else {
                #[expect(clippy::cast_sign_loss)]
                Ok(Some(LongInt::left_shift_i64(lhs, rhs as u64, vm)?))
            }
        } else if let Self::Ref(id) = self {
            vm.heap.read(*id).py_lshift_impl(other, vm)
        } else {
            Ok(None)
        }
    }

    /// Reflected implementation of Python `<<`.
    fn py_rlshift_impl(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        if let Self::Ref(id) = self {
            vm.heap.read(*id).py_rlshift_impl(other, vm)
        } else {
            Ok(None)
        }
    }

    /// One-sided implementation of Python `>>`.
    fn py_rshift_impl(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        if let (Some(lhs), Some(rhs)) = (immediate_int(self), immediate_int(other)) {
            if rhs < 0 {
                Err(ExcType::value_error_negative_shift_count())
            } else {
                let value = u32::try_from(rhs)
                    .ok()
                    .filter(|shift| *shift < 64)
                    .map_or_else(|| if lhs < 0 { -1 } else { 0 }, |shift| lhs >> shift);
                Ok(Some(Self::Int(value)))
            }
        } else if let Self::Ref(id) = self {
            vm.heap.read(*id).py_rshift_impl(other, vm)
        } else {
            Ok(None)
        }
    }

    /// Reflected implementation of Python `>>`.
    fn py_rrshift_impl(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        if let Self::Ref(id) = self {
            vm.heap.read(*id).py_rrshift_impl(other, vm)
        } else {
            Ok(None)
        }
    }

    /// One-sided implementation of matrix multiplication.
    fn py_matmul_impl(&self, _other: &Self, _vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        Err(ExcType::not_implemented("matrix multiplication (@) is not supported").into())
    }

    /// Reflected implementation of matrix multiplication.
    fn py_rmatmul_impl(&self, _other: &Self, _vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        Ok(None)
    }

    fn py_getitem(&self, key: &Self, vm: &mut VM<'_>) -> RunResult<Self> {
        let interns = vm.interns;
        match self {
            // `heap_subscript` owns the one case a heap read cannot: a
            // defaultdict miss, which calls its factory and re-enters the VM.
            Self::Ref(id) => heap_subscript(*id, key, vm),
            Self::InternString(string_id) => {
                // Check for slice first
                if let Self::Ref(key_id) = key
                    && let HeapData::Slice(slice_obj) = vm.heap.get(*key_id)
                {
                    let s = interns.get_str(*string_id);
                    let result_str: Box<str> = slice_collect_iterator(vm, slice_obj, s.chars(), |c| c)?;
                    return Ok(allocate_string(result_str, vm.heap));
                }

                // Handle interned string indexing, accepting Int and Bool
                let index = match key {
                    Self::Int(i) => *i,
                    Self::Bool(b) => i64::from(*b),
                    _ => return Err(ExcType::type_error_indices(Type::Str, &key.py_type_name(vm))),
                };

                let s = interns.get_str(*string_id);
                let c = get_char_at_index(s, index).ok_or_else(ExcType::str_index_error)?;
                Ok(allocate_char(c, vm.heap))
            }
            Self::InternBytes(bytes_id) => {
                // Check for slice first
                if let Self::Ref(key_id) = key
                    && let HeapData::Slice(slice_obj) = vm.heap.get(*key_id)
                {
                    let bytes = interns.get_bytes(*bytes_id);
                    let result_bytes = slice_collect_iterator(vm, slice_obj, bytes.iter(), |b| *b)?;
                    let heap_id = vm.heap.allocate(HeapData::Bytes(Bytes::new(result_bytes)));
                    return Ok(Self::Ref(heap_id));
                }

                // Handle interned bytes indexing - returns integer byte value
                let index = match key {
                    Self::Int(i) => *i,
                    Self::Bool(b) => i64::from(*b),
                    _ => return Err(ExcType::type_error_indices(Type::Bytes, &key.py_type_name(vm))),
                };

                let bytes = interns.get_bytes(*bytes_id);
                let byte = get_byte_at_index(bytes, index).ok_or_else(ExcType::bytes_index_error)?;
                Ok(Self::Int(i64::from(byte)))
            }
            _ => Err(ExcType::type_error_not_sub(&self.py_type_name(vm))),
        }
    }

    fn py_setitem(&mut self, key: Self, value: Self, vm: &mut VM<'_>) -> RunResult<()> {
        match self {
            Self::Ref(id) => vm.heap.read(*id).py_setitem(key, value, vm),
            _ => Err(ExcType::type_error(format!(
                "'{}' object does not support item assignment",
                self.py_type_name(vm)
            ))),
        }
    }

    fn py_is_iterator(&self, vm: &VM<'_>) -> bool {
        // No immediate value is an iterator; interned `str`/`bytes` are iterable
        // but, as in CPython, are not their own iterators.
        match self {
            Self::Ref(id) => vm.heap.read(*id).py_is_iterator(vm),
            _ => false,
        }
    }

    fn py_is_iterable(&self, vm: &VM<'_>) -> bool {
        match self {
            // Interned string and bytes literals iterate without ever reaching
            // the heap, so they answer here rather than in `HeapReadOutput`.
            Self::InternString(_) | Self::InternBytes(_) => true,
            Self::Ref(id) => vm.heap.read(*id).py_is_iterable(vm),
            _ => false,
        }
    }

    fn py_iter(&self, _: Option<HeapId>, vm: &mut VM<'_>) -> RunResult<Self> {
        if let Self::Ref(id) = self {
            vm.heap.read(*id).py_iter(Some(*id), vm)
        } else {
            match self {
                Self::InternString(id) => Ok(StringIterator::from_intern(*id, vm)),
                Self::InternBytes(id) => Ok(BytesIterator::from_intern(*id, vm)),
                _ => Err(ExcType::type_error_not_iterable(&self.py_type_name(vm))),
            }
        }
    }

    fn py_next(&mut self, _: Option<HeapId>, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        if let Self::Ref(id) = self {
            vm.heap.read(*id).py_next(Some(*id), vm)
        } else {
            Err(ExcType::type_error_not_iterator(&self.py_type_name(vm)))
        }
    }
}

/// `Value` releases its (possible) heap reference through any [`ContainsHeap`]
/// context — `Heap`, `HeapReader`, `VM`, or the json `Encoder`. Forwards to the
/// inherent [`Value::drop_with`], which also serves direct callers.
impl<C: ContainsHeap> DropWithContext<C> for Value {
    #[inline]
    fn drop_with(self, ctx: &mut C) {
        // Resolves to the inherent `Value::drop_with` (inherent methods take
        // priority), not this trait method — so no recursion.
        Self::drop_with(self, ctx);
    }
}

impl Value {
    /// Returns the Python `Type` for this value using only `&Heap` (no full VM borrow).
    ///
    /// Wraps [`py_type_shallow`](Self::py_type_shallow) for immediate values and
    /// delegates to [`HeapData::py_type`] for `Value::Ref`. Useful in code paths
    /// that need a type label (e.g. CPython-style "argument N must be X, not Y"
    /// errors) but don't have a `&VM` handy — notably the macro-generated
    /// `from_args` bodies, which are passed `heap` + `interns` rather than a VM.
    #[must_use]
    pub(crate) fn py_type_heap(&self, heap: &Heap) -> Type {
        match self {
            Self::Ref(id) => heap.get(*id).py_type(),
            _ => self.py_type_shallow(),
        }
    }

    /// Resolved display name of this value's type for error messages and
    /// reprs — user-class instances render as their real class name rather
    /// than the generic `"object"`.
    ///
    /// The result borrows only `vm.interns` (never the heap), so it can be
    /// captured before `drop_with` cleanup and formatted after.
    #[must_use]
    pub(crate) fn py_type_name<'h>(&self, vm: &VM<'h>) -> Cow<'h, str> {
        self.namedtuple_class_name(vm.heap, vm.interns)
            .unwrap_or_else(|| self.py_type(vm).name(vm.heap, vm.interns))
    }

    /// [`py_type_name`](Self::py_type_name) for contexts without a `&VM` —
    /// notably the macro-generated `from_args` bodies, which are passed
    /// `heap` + `interns` instead (mirrors [`py_type_heap`](Self::py_type_heap)).
    #[must_use]
    pub(crate) fn py_type_name_heap<'i>(&self, heap: &Heap, interns: &'i Interns) -> Cow<'i, str> {
        self.namedtuple_class_name(heap, interns)
            .unwrap_or_else(|| self.py_type_heap(heap).name(heap, interns))
    }

    /// Class name of a named tuple, if this value is one.
    ///
    /// Named tuples keep their class name in the heap entry rather than in
    /// [`Type`], which carries no identity for them. Error messages therefore
    /// have to reach for it here to name the class (`'P'`) rather than the
    /// generic `'namedtuple'`, matching CPython — including for structseqs,
    /// whose stored name is already the qualified `sys.version_info`.
    #[must_use]
    fn namedtuple_class_name<'i>(&self, heap: &Heap, interns: &'i Interns) -> Option<Cow<'i, str>> {
        if let Self::Ref(heap_id) = self
            && let HeapData::NamedTuple(nt) = heap.get(*heap_id)
        {
            Some(match nt.name_either() {
                EitherStr::Interned(id) => Cow::Borrowed(interns.get_str(*id)),
                EitherStr::Heap(s) => Cow::Owned(s.clone()),
            })
        } else {
            None
        }
    }

    /// Returns the Python `Type` for immediate (non-heap) values without VM access.
    ///
    /// For `Value::Ref` variants this cannot determine the concrete type (that requires
    /// reading from the heap), so it falls back to `Type::NoneType` as a sentinel.
    /// Callers handling `Ref` should use `HeapData::py_type()` on the resolved data instead.
    #[must_use]
    pub(crate) fn py_type_shallow(&self) -> Type {
        match self {
            Self::Undefined | Self::None => Type::NoneType,
            Self::Ellipsis => Type::Ellipsis,
            Self::NotImplemented => Type::NotImplementedType,
            Self::Bool(_) => Type::Bool,
            Self::Int(_) | Self::InternLongInt(_) => Type::Int,
            Self::Float(_) => Type::Float,
            Self::InternString(_) => Type::Str,
            Self::InternBytes(_) => Type::Bytes,
            Self::Builtin(_) => Type::BuiltinFunction,
            Self::ModuleFunction(_) | Self::DefFunction(_) => Type::Function,
            Self::Marker(_) => Type::SpecialForm,
            Self::Property(_) => Type::Property,
            Self::Ref(_) => Type::NoneType, // callers should resolve Ref via HeapData::py_type()
            #[cfg(feature = "memory-model-checks")]
            Self::Dereferenced => Type::NoneType,
        }
    }

    /// Returns this value's complete structural identity.
    ///
    /// Immediate values retain Monty's value-derived identity, while heap objects
    /// use their arena index.
    pub(crate) fn id(&self) -> Identity {
        Identity::new(self)
    }

    /// Returns the Ref ID if this value is a reference, otherwise returns None.
    pub fn ref_id(&self) -> Option<HeapId> {
        match self {
            Self::Ref(id) => Some(*id),
            _ => None,
        }
    }

    /// Consumes this Value as a reference, returning the contained HeapId otherwise returns None.
    ///
    /// The caller is responsible for ensuring the `HeapId` is properly decref'd.
    pub fn into_ref_id(self) -> Option<HeapId> {
        match self {
            Self::Ref(id) => {
                #[cfg(feature = "memory-model-checks")]
                {
                    // Mark the value as dereferenced to prevent double-free
                    mem::forget(self);
                }
                Some(id)
            }
            _ => None,
        }
    }

    /// Returns the module name if this value is a module, otherwise returns "<unknown>".
    ///
    /// Used for error messages in `from module import name` when the name doesn't exist.
    pub fn module_name(&self, vm: &mut VM<'_>) -> String {
        match self {
            Self::Ref(id) => match vm.heap.get(*id) {
                HeapData::Module(module) => vm.interns.get_str(module.name()).to_string(),
                _ => "<unknown>".to_string(),
            },
            _ => "<unknown>".to_string(),
        }
    }

    /// Python-visible `is` operator using complete structural identity.
    ///
    /// Values compare using their full immediate or arena identity.
    pub fn is(&self, other: &Self) -> bool {
        self.id() == other.id()
    }

    /// Whether this value is Python's `NotImplemented` singleton.
    #[must_use]
    pub(crate) fn is_not_implemented(&self) -> bool {
        matches!(self, Self::NotImplemented)
    }

    /// Equality as containers perform it: identity before user equality.
    pub fn py_eq(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<bool> {
        if self.is(other) {
            Ok(true)
        } else {
            self.py_eq_operator(other, vm)
        }
    }

    /// Python's `==` operator normalized to a boolean.
    pub fn py_eq_operator(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<bool> {
        let result = self.py_rich_eq(other, vm)?;
        defer_drop!(result, vm);
        result.py_bool(vm)
    }

    /// Direct Python `==`, preserving any arbitrary value returned by `__eq__`.
    ///
    /// Tries the left operand, then reflected equality when it returns
    /// `NotImplemented`; identity is only the final fallback after both decline.
    pub(crate) fn py_rich_eq(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Self> {
        let lhs_result = self.py_rich_eq_impl(other, vm)?;
        if !lhs_result.is_not_implemented() {
            return Ok(lhs_result);
        }

        let rhs_result = other.py_rich_eq_impl(self, vm)?;
        if !rhs_result.is_not_implemented() {
            return Ok(rhs_result);
        }

        Ok(Self::Bool(self.is(other)))
    }

    /// Runs one side of rich equality, using `NotImplemented` to request reflected dispatch.
    fn py_rich_eq_impl(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Self> {
        if let Self::Ref(id) = self
            && matches!(vm.heap.get(*id), HeapData::Instance(_))
        {
            if let Some(result) = instance_user_eq(*id, other, vm)? {
                Ok(result)
            } else if self.is(other) {
                Ok(Self::Bool(true))
            } else if let Some(result) = instance_dataclass_eq(*id, other, vm)? {
                Ok(Self::Bool(result))
            } else {
                Ok(Self::NotImplemented)
            }
        } else if let Some(result) = self.py_eq_impl(other, vm)? {
            Ok(Self::Bool(result))
        } else {
            Ok(Self::NotImplemented)
        }
    }

    /// Returns an iterator for this value using its type-specific protocol when available.
    pub fn py_iter(&self, vm: &mut VM<'_>) -> RunResult<Self> {
        <Self as PyTrait<'_>>::py_iter(self, None, vm)
    }

    /// Advances this value as an iterator, mirroring the inherent [`Value::py_iter`].
    ///
    /// The trait method's `self_id` is redundant for a `Value`, which already
    /// carries its own `HeapId`, so callers use this and let the impl resolve it.
    pub(crate) fn py_next(&mut self, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        <Self as PyTrait<'_>>::py_next(self, None, vm)
    }

    /// Converts an owned value into its Python iterator and releases the original reference.
    ///
    /// This is the ownership-preserving entry point for Rust consumers of the
    /// iteration protocol; the returned iterator retains its source as needed.
    pub(crate) fn into_py_iter(self, vm: &mut VM<'_>) -> RunResult<Self> {
        let iterator = self.py_iter(vm);
        self.drop_with(vm);
        iterator
    }

    /// Creates a scoped view that retains this value's heap reader when needed.
    pub(crate) fn read<'h, 'v>(&'v self, vm: &VM<'h>) -> ValueRead<'h, 'v> {
        match self {
            Self::Ref(id) => ValueRead::Heap {
                owner: self,
                value: vm.heap.read(*id),
            },
            _ => ValueRead::Immediate(self),
        }
    }

    /// Reads the heap entry this value references, or `None` if it is not a
    /// heap reference.
    ///
    /// Used by per-type [`PyTrait::py_eq_impl`] impls to resolve the other operand
    /// to a heap object of their own type, returning `NotImplemented` otherwise.
    pub(crate) fn read_heap<'a>(&self, vm: &VM<'a>) -> Option<HeapReadOutput<'a>> {
        match self {
            Self::Ref(id) => Some(vm.heap.read(*id)),
            _ => None,
        }
    }

    /// Computes the hash value for this value, used for dict keys.
    ///
    /// Returns `Ok(Some(hash))` for hashable types (immediate values and immutable heap types).
    /// Returns `Ok(None)` for unhashable types (list, dict).
    /// Returns `Err(ResourceError::Recursion)` if the recursion limit is exceeded
    /// while hashing deeply nested containers (e.g., tuples of tuples).
    ///
    /// For heap-allocated values (Ref variant), this computes the hash lazily
    /// on first use and caches it for subsequent calls.
    pub fn py_hash(&self, vm: &mut VM<'_>) -> RunResult<Option<HashValue>> {
        // The hot arms (int/str/ref) return precomputed or cached hashes; only the
        // cold arms construct a hasher, via `hash_one`.
        match self {
            Self::InternString(string_id) => Ok(Some(vm.interns.str_hash(*string_id))),
            Self::InternBytes(bytes_id) => Ok(Some(vm.interns.bytes_hash(*bytes_id))),
            Self::InternLongInt(long_int_id) => Ok(Some(vm.interns.long_int_hash(*long_int_id))),
            // Bool and int hash directly as their value, and are equivalent
            Self::Bool(b) => Ok(Some(HashValue::new((*b).into()))),
            Self::Int(i) => Ok(Some(HashValue::new(i.cast_unsigned()))),
            Self::Float(f) => {
                // 2^63, the first power of two past i64::MAX (exactly representable).
                const TWO_POW_63: f64 = 9_223_372_036_854_775_808.0;
                if f.fract() != 0.0 || !f.is_finite() {
                    // Non-integral or non-finite: hash the bit representation.
                    Ok(Some(HashValue::new(f.to_bits())))
                } else if *f >= -TWO_POW_63 && *f < TWO_POW_63 {
                    // Integral float in i64 range hashes as the equivalent int
                    // (e.g. `1.0` hashes the same as `1`).
                    #[expect(clippy::cast_possible_truncation)]
                    Ok(Some(HashValue::new((*f as i64).cast_unsigned())))
                } else {
                    // Integral float outside i64 range hashes as the equivalent
                    // big int, so an exactly-equal `float`/`int` pair (e.g.
                    // `2.0**100 == 2**100`) preserves `hash(a) == hash(b)`.
                    Ok(Some(hash_python_long_int(
                        &BigInt::from_f64(*f).expect("finite f64 converts to BigInt"),
                    )))
                }
            }
            // For heap-allocated values, dispatch to the per-type `py_hash`
            // impl. Types that benefit from caching (Str/Bytes/Tuple/
            // NamedTuple/FrozenSet/Path) carry an inline `cached_hash`;
            // cheap-to-hash types recompute each call.
            Self::Ref(id) => vm.heap.read(*id).py_hash(*id, vm),
            // Singleton values hash by discriminant
            Self::Undefined | Self::Ellipsis | Self::NotImplemented | Self::None => {
                Ok(Some(hash_one(discriminant(self))))
            }
            Self::Builtin(b) => Ok(Some(hash_one(b))),
            Self::ModuleFunction(mf) => Ok(Some(hash_one(mf))),
            // Hash functions based on function ID
            Self::DefFunction(f_id) => Ok(Some(hash_one(f_id))),
            // Markers are hashable based on their discriminant
            Self::Marker(m) => Ok(Some(hash_one(m))),
            // Properties are hashable based on their OS function discriminant
            Self::Property(p) => Ok(Some(hash_one(p))),
            #[cfg(feature = "memory-model-checks")]
            Self::Dereferenced => panic!("Cannot access Dereferenced object"),
        }
    }

    /// Checks if `item` is contained in `self` (the container) — Python's `in`.
    ///
    /// Type-specific logic lives on each type's [`PyTrait::py_contains`]; this
    /// resolves the value and applies CPython's protocol order: a type's own
    /// containment first (`sq_contains`), then consuming it by iteration
    /// (`tp_iter`), then `TypeError`.
    pub fn py_contains(&self, item: &Self, vm: &mut VM<'_>) -> RunResult<bool> {
        match self {
            Self::Ref(heap_id) => match vm.heap.read(*heap_id).py_contains_impl(*heap_id, item, vm)? {
                Some(found) => Ok(found),
                None if self.py_is_iterable(vm) => self.contains_by_iteration(item, vm),
                None => Err(ExcType::type_error_not_container(&self.py_type_name(vm))),
            },
            // Interned strings and bytes never reach the heap, so they answer here.
            Self::InternString(string_id) => {
                let container_str = vm.interns.get_str(*string_id);
                str_contains(container_str, item, vm.heap, vm.interns)
            }
            Self::InternBytes(bytes_id) => {
                let container = vm.interns.get_bytes(*bytes_id);
                bytes_contains(container, item, vm)
            }
            _ => Err(ExcType::type_error_not_container(&self.py_type_name(vm))),
        }
    }

    /// Consumes `self` as an iterable, reporting whether `item` compares equal
    /// to any element — the `in` fallback for anything without `__contains__`.
    ///
    /// Only called for values already known to be iterable, so it does not
    /// re-check. An endless iterable runs until a resource limit fires.
    fn contains_by_iteration(&self, item: &Self, vm: &mut VM<'_>) -> RunResult<bool> {
        let iter = self.py_iter(vm)?;
        defer_drop!(iter, vm);
        let mut iter = iter.read(vm);
        loop {
            let Some(el) = iter.py_next(vm)? else {
                break Ok(false);
            };
            let eq = item.py_eq(&el, vm);
            el.drop_with(vm);
            if eq? {
                break Ok(true);
            }
        }
    }

    /// Gets an attribute from this value.
    ///
    /// Dispatches to `py_getattr` on the underlying types where appropriate.
    /// Accepts `EitherStr` to support both interned and heap-allocated attribute names.
    ///
    /// Returns `AttributeError` for other types or unknown attributes.
    pub fn py_getattr(&self, attr: &EitherStr, vm: &mut VM<'_>) -> RunResult<CallResult> {
        match self {
            // Instances resolve attributes (instance dict → class methods/vars,
            // binding methods) in a dedicated path that has the heap id needed to
            // build bound methods.
            Self::Ref(heap_id) if matches!(vm.heap.get(*heap_id), HeapData::Instance(_)) => {
                return instance_getattr(*heap_id, attr, vm);
            }
            Self::Ref(heap_id) => {
                if let Some(call_result) = vm.heap.read(*heap_id).py_getattr(attr, vm)? {
                    return Ok(call_result);
                }
            }
            Self::Builtin(Builtins::Type(t)) => {
                // Handle type object attributes like __name__
                let is_dunder_name = attr.static_string().map_or_else(
                    || attr.as_str(vm.interns) == "__name__",
                    |ss| ss == StaticStrings::DunderName,
                );
                if is_dunder_name {
                    return Ok(CallResult::Value(allocate_string(t.name(vm.heap, vm.interns), vm.heap)));
                }
                if *t == Type::TimeZone && attr.as_str(vm.interns) == "utc" {
                    return Ok(CallResult::Value(vm.heap.get_timezone_utc()));
                }
            }
            _ => {}
        }
        let type_name = self.py_type_name(vm);
        Err(ExcType::attribute_error(type_name, attr.as_str(vm.interns)))
    }

    /// Sets an attribute, consuming `value` on both success and error.
    pub fn py_set_attr(&self, name: &EitherStr, value: Self, vm: &mut VM<'_>) -> RunResult<()> {
        if let Self::Ref(heap_id) = self {
            vm.heap.read(*heap_id).py_set_attr(name, value, vm)
        } else {
            value.drop_with(vm);
            let type_name = self.py_type_name(vm);
            Err(ExcType::attribute_error_no_setattr(&type_name, name.as_str(vm.interns)))
        }
    }

    /// Extracts an integer value from the Value.
    ///
    /// Accepts `Int` and `LongInt` (if it fits in i64). Returns a `TypeError` for other types
    /// and an `OverflowError` if the `LongInt` value is too large.
    ///
    /// Note: The LongInt-to-i64 conversion path is defensive code. In normal execution,
    /// heap-allocated `LongInt` values always exceed i64 range because `LongInt::into_value()`
    /// automatically demotes i64-fitting values to `Value::Int`. However, this path could be
    /// reached via deserialization of crafted snapshot data.
    pub fn as_int(&self, vm: &VM<'_>) -> RunResult<i64> {
        match self {
            Self::Int(i) => Ok(*i),
            Self::Ref(heap_id) => {
                if let HeapData::LongInt(li) = vm.heap.get(*heap_id) {
                    li.to_i64().ok_or_else(ExcType::overflow_c_ssize_t)
                } else {
                    let msg = format!("'{}' object cannot be interpreted as an integer", self.py_type_name(vm));
                    Err(SimpleException::new_msg(ExcType::TypeError, msg).into())
                }
            }
            _ => {
                let msg = format!("'{}' object cannot be interpreted as an integer", self.py_type_name(vm));
                Err(SimpleException::new_msg(ExcType::TypeError, msg).into())
            }
        }
    }

    /// Extracts an index value for sequence operations.
    ///
    /// Accepts `Int`, `Bool` (True=1, False=0), and `LongInt` (if it fits in i64).
    /// Returns a `TypeError` for other types with the container type name included.
    /// Returns an `IndexError` if the `LongInt` value is too large to use as an index.
    ///
    /// Note: The LongInt-to-i64 conversion path is defensive code. In normal execution,
    /// heap-allocated `LongInt` values always exceed i64 range because `LongInt::into_value()`
    /// automatically demotes i64-fitting values to `Value::Int`. However, this path could be
    /// reached via deserialization of crafted snapshot data.
    pub fn as_index(&self, vm: &VM<'_>, container_type: Type) -> RunResult<i64> {
        match self {
            Self::Int(i) => Ok(*i),
            Self::Bool(b) => Ok(i64::from(*b)),
            Self::Ref(heap_id) => {
                if let HeapData::LongInt(li) = vm.heap.get(*heap_id) {
                    li.to_i64().ok_or_else(ExcType::index_error_int_too_large)
                } else {
                    Err(ExcType::type_error_indices(container_type, &self.py_type_name(vm)))
                }
            }
            _ => Err(ExcType::type_error_indices(container_type, &self.py_type_name(vm))),
        }
    }

    /// True when this is a `LongInt`-valued int (interned or heap-allocated)
    /// that is negative — lets fixed-width consumers pick the right overflow
    /// direction/message (`round`'s i64 clamp, `os`'s fd converter). False for
    /// every other value, `Int`/`Bool` included: pair it with
    /// [`is_long_int`](crate::args::is_long_int) rather than using it alone to
    /// classify an int.
    pub(crate) fn long_int_is_negative(&self, vm: &VM<'_>) -> bool {
        match self {
            Self::InternLongInt(id) => vm.interns.get_long_int(*id).sign() == Sign::Minus,
            Self::Ref(id) => matches!(vm.heap.get(*id), HeapData::LongInt(li) if li.is_negative()),
            _ => false,
        }
    }

    /// Performs Python `+` with reflected-operation fallback.
    pub(crate) fn py_add(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Self> {
        if let Some(result) = self.py_add_result(other, vm)? {
            Ok(result)
        } else {
            let lhs_type = self.py_type(vm);
            Err(ExcType::binary_type_error(
                "+",
                lhs_type,
                self.py_type_name(vm),
                other.py_type_name(vm),
            ))
        }
    }

    /// Tries direct and reflected addition without producing the final type error.
    pub(crate) fn py_add_result(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Option<Self>> {
        if let Some(result) = self.py_add_impl(other, vm, self.ref_id())? {
            Ok(Some(result))
        } else {
            other.py_radd_impl(self, vm)
        }
    }

    /// Performs Python `-` with reflected-operation fallback.
    pub(crate) fn py_sub(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Self> {
        self.binary_op(
            other,
            vm,
            |vm| self.py_sub_impl(other, vm, self.ref_id()),
            |vm| other.py_rsub_impl(self, vm),
            "-",
        )
    }

    /// Performs Python `*` with reflected-operation fallback.
    pub(crate) fn py_mul(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Self> {
        self.binary_op(
            other,
            vm,
            |vm| self.py_mul_impl(other, vm),
            |vm| other.py_rmul_impl(self, vm),
            "*",
        )
    }

    /// Performs Python `@` with reflected-operation fallback.
    pub(crate) fn py_matmul(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Self> {
        self.binary_op(
            other,
            vm,
            |vm| self.py_matmul_impl(other, vm),
            |vm| other.py_rmatmul_impl(self, vm),
            "@",
        )
    }

    /// Performs Python `/` with reflected-operation fallback.
    pub(crate) fn py_truediv(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Self> {
        self.binary_op(
            other,
            vm,
            |vm| self.py_truediv_impl(other, vm),
            |vm| other.py_rtruediv_impl(self, vm),
            "/",
        )
    }

    /// Performs Python `//` with reflected-operation fallback.
    pub(crate) fn py_floordiv(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Self> {
        self.binary_op(
            other,
            vm,
            |vm| self.py_floordiv_impl(other, vm),
            |vm| other.py_rfloordiv_impl(self, vm),
            "//",
        )
    }

    /// Performs Python `%` with reflected-operation fallback.
    pub(crate) fn py_mod(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Self> {
        self.binary_op(
            other,
            vm,
            |vm| self.py_mod_impl(other, vm),
            |vm| other.py_rmod_impl(self, vm),
            "%",
        )
    }

    /// Performs Python `** or pow()` with reflected-operation fallback.
    pub(crate) fn py_pow(&self, other: &Self, modulus: Option<&Self>, vm: &mut VM<'_>) -> RunResult<Self> {
        self.binary_op(
            other,
            vm,
            |vm| self.py_pow_impl(other, modulus, vm),
            |vm| other.py_rpow_impl(self, modulus, vm),
            "** or pow()",
        )
    }

    /// Performs Python `&` with reflected-operation fallback.
    pub(crate) fn py_and(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Self> {
        self.binary_op(
            other,
            vm,
            |vm| self.py_and_impl(other, vm, self.ref_id()),
            |vm| other.py_rand_impl(self, vm),
            "&",
        )
    }

    /// Performs Python `|` with reflected-operation fallback.
    pub(crate) fn py_or(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Self> {
        self.binary_op(
            other,
            vm,
            |vm| self.py_or_impl(other, vm, self.ref_id()),
            |vm| other.py_ror_impl(self, vm),
            "|",
        )
    }

    /// Performs Python `^` with reflected-operation fallback.
    pub(crate) fn py_xor(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Self> {
        self.binary_op(
            other,
            vm,
            |vm| self.py_xor_impl(other, vm),
            |vm| other.py_rxor_impl(self, vm),
            "^",
        )
    }

    /// Performs Python `<<` with reflected-operation fallback.
    pub(crate) fn py_lshift(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Self> {
        self.binary_op(
            other,
            vm,
            |vm| self.py_lshift_impl(other, vm),
            |vm| other.py_rlshift_impl(self, vm),
            "<<",
        )
    }

    /// Performs Python `>>` with reflected-operation fallback.
    pub(crate) fn py_rshift(&self, other: &Self, vm: &mut VM<'_>) -> RunResult<Self> {
        self.binary_op(
            other,
            vm,
            |vm| self.py_rshift_impl(other, vm),
            |vm| other.py_rrshift_impl(self, vm),
            ">>",
        )
    }

    /// Applies the shared direct, reflected, then unsupported binary protocol.
    fn binary_op(
        &self,
        other: &Self,
        vm: &mut VM<'_>,
        direct: impl FnOnce(&mut VM<'_>) -> RunResult<Option<Self>>,
        reflected: impl FnOnce(&mut VM<'_>) -> RunResult<Option<Self>>,
        operator: &'static str,
    ) -> RunResult<Self> {
        if let Some(result) = direct(vm)? {
            Ok(result)
        } else if let Some(result) = reflected(vm)? {
            Ok(result)
        } else {
            let lhs_type = self.py_type(vm);
            let lhs_name = self.py_type_name(vm);
            Err(ExcType::binary_type_error(
                operator,
                lhs_type,
                lhs_name,
                other.py_type_name(vm),
            ))
        }
    }

    /// Clones a value with proper heap reference counting.
    ///
    /// For immediate values (Int, Bool, None, etc.), this performs a simple copy.
    /// For heap-allocated values (Ref variant), this increments the reference count
    /// and returns a new reference to the same heap value.
    ///
    /// Takes `ContainsHeap` to allow directly passing the `VM` in many contexts. Where
    /// borrow checking creates conflicts, it may be preferred to pass `&Heap` directly
    /// (e.g. as `vm.heap` / `self.heap` etc.).
    ///
    /// # Important
    /// This method MUST be used instead of the derived `Clone` implementation to ensure
    /// proper reference counting. Using `.clone()` directly will bypass reference counting
    /// and cause memory leaks or double-frees.
    #[must_use]
    pub fn clone_with_heap(&self, heap: &impl ContainsHeap) -> Self {
        match self {
            Self::Undefined => Self::Undefined,
            Self::Ellipsis => Self::Ellipsis,
            Self::NotImplemented => Self::NotImplemented,
            Self::None => Self::None,
            Self::Bool(b) => Self::Bool(*b),
            Self::Int(v) => Self::Int(*v),
            Self::Float(v) => Self::Float(*v),
            Self::Builtin(b) => Self::Builtin(*b),
            Self::ModuleFunction(mf) => Self::ModuleFunction(*mf),
            Self::DefFunction(f) => Self::DefFunction(*f),
            Self::InternString(s) => Self::InternString(*s),
            Self::InternBytes(b) => Self::InternBytes(*b),
            Self::InternLongInt(bi) => Self::InternLongInt(*bi),
            Self::Marker(m) => Self::Marker(*m),
            Self::Property(p) => Self::Property(*p),
            Self::Ref(id) => {
                heap.heap().inc_ref(*id);
                Self::Ref(*id)
            }
            #[cfg(feature = "memory-model-checks")]
            Self::Dereferenced => panic!("Cannot copy Dereferenced object"),
        }
    }

    /// Drops an value, decrementing its heap reference count if applicable.
    ///
    /// For immediate values, this is a no-op. For heap-allocated values (Ref variant),
    /// this decrements the reference count and frees the value (and any children) when
    /// the count reaches zero. For Closure variants, this decrements ref counts on all
    /// captured cells.
    ///
    /// Takes `ContainsHeap` to allow directly passing the `VM` in many contexts. Where
    /// borrow checking creates conflicts, it may be preferred to pass `&mut Heap` directly
    /// (e.g. as `vm.heap` / `self.heap` etc.).
    ///
    /// # Important
    /// This method MUST be called before overwriting a namespace slot or discarding
    /// a value to prevent memory leaks. Call it directly only on a simple, linear
    /// cleanup path; use `defer_drop!` or `DropGuard` when any branch or `?` could
    /// bypass cleanup.
    #[cfg(not(feature = "memory-model-checks"))]
    #[inline]
    pub fn drop_with(self, heap: &mut impl ContainsHeap) {
        if let Self::Ref(id) = self {
            heap.heap_mut().dec_ref(id);
        }
    }
    /// With `memory-model-checks` enabled, `Ref` variants are replaced with `Dereferenced` and
    /// the original is forgotten to prevent the Drop impl from panicking. Non-Ref variants
    /// are left unchanged since they don't trigger the Drop panic.
    #[cfg(feature = "memory-model-checks")]
    pub fn drop_with(mut self, heap: &mut impl ContainsHeap) {
        let old = mem::replace(&mut self, Self::Dereferenced);
        if let Self::Ref(id) = &old {
            heap.heap_mut().dec_ref(*id);
            mem::forget(old);
        }
    }

    /// Mark as Dereferenced to prevent Drop panic
    ///
    /// This should be called from `py_dec_ref_ids` methods only
    #[cfg(feature = "memory-model-checks")]
    pub fn dec_ref_forget(&mut self) {
        let old = mem::replace(self, Self::Dereferenced);
        mem::forget(old);
    }

    /// Pushes any contained `HeapId` onto the stack for reference counting.
    ///
    /// For `Value::Ref` variants, pushes the heap ID so the referenced object's
    /// refcount can be decremented. When `memory-model-checks` is enabled, also marks
    /// this value as `Dereferenced` to prevent Drop panics.
    pub fn py_dec_ref_ids(&mut self, stack: &mut Vec<HeapId>) {
        if let Self::Ref(id) = self {
            stack.push(*id);
            #[cfg(feature = "memory-model-checks")]
            self.dec_ref_forget();
        }
    }

    /// Converts the value into a keyword string representation if possible.
    ///
    /// Returns `Some(KeywordStr)` for `InternString` values or heap `str`
    /// objects, otherwise returns `None`.
    pub fn as_either_str(&self, heap: &Heap) -> Option<EitherStr> {
        match self {
            Self::InternString(id) => Some(EitherStr::Interned(*id)),
            Self::Ref(heap_id) => match heap.get(*heap_id) {
                HeapData::Str(s) => Some(EitherStr::Heap(s.as_str().to_owned())),
                _ => None,
            },
            _ => None,
        }
    }

    /// Borrows the value as a `&str` if it is a string (interned or heap `str`).
    ///
    /// Unlike [`as_either_str`](Self::as_either_str) this never allocates — both
    /// interned and heap strings are returned by borrow — and it errors rather
    /// than returning `None` for non-strings, with CPython's generic `expected
    /// string, not <type>` message. Use it wherever a function reads a `str`
    /// argument it does not need to own; the borrow keeps `self` and the heap
    /// pinned, so drop/allocate only once it ends.
    pub(crate) fn to_str<'a>(&'a self, vm: &'a VM<'_>) -> RunResult<&'a str> {
        self.to_str_heap(vm.heap, vm.interns)
    }

    /// [`to_str`](Self::to_str) for contexts without a `&VM` — takes `heap` and
    /// `interns` separately so callers can keep a disjoint `&mut` borrow of
    /// another `VM` field alive (e.g. resolving a `str` `Value` produced by
    /// `py_str` while writing it to `vm.print_writer`).
    pub(crate) fn to_str_heap<'a>(&'a self, heap: &'a Heap, interns: &'a Interns) -> RunResult<&'a str> {
        match self {
            Self::InternString(string_id) => return Ok(interns.get_str(*string_id)),
            Self::Ref(heap_id) => {
                if let HeapData::Str(s) = heap.get(*heap_id) {
                    return Ok(s.as_str());
                }
            }
            _ => {}
        }
        Err(ExcType::type_error(format!(
            "expected string, not {}",
            self.py_type_name_heap(heap, interns)
        )))
    }

    /// check if the value is a string.
    pub fn is_str(&self, heap: &Heap) -> bool {
        match self {
            Self::InternString(_) => true,
            Self::Ref(heap_id) => matches!(heap.get(*heap_id), HeapData::Str(_)),
            _ => false,
        }
    }

    /// Whether calling this value would succeed at dispatch.
    ///
    /// Dispatch can't double as the predicate — it pushes a frame, clones
    /// defaults, constructs an instance — so the two are kept in lockstep by
    /// `debug_assert!`s in dispatch's "not callable" arms.
    pub(crate) fn is_callable(&self, heap: &Heap) -> bool {
        match self {
            Self::Builtin(_) | Self::ModuleFunction(_) | Self::DefFunction(_) => true,
            Self::Ref(id) => heap.get(*id).is_callable(),
            _ => false,
        }
    }
}

// ---------------------------------------------------------------------------
// Shared one-sided equality helpers
//
// Each compares a primitive operand (extracted from either an inline `Value`
// or a heap object) against an arbitrary `other: &Value`, resolving `other`'s
// representation as needed. They return `None` (NotImplemented) when `other`
// is not a compatible Python type, so the reflected comparison can run. These
// are shared by `Value::py_eq_impl` (inline operands) and
// `HeapReadOutput::py_eq_impl` (heap operands) so the interned-vs-heap and
// numeric-tower logic lives once.
// ---------------------------------------------------------------------------

/// `a == other` over Python's numeric tower (`int`/`bool`/`float`/big `int`).
pub(crate) fn eq_i64(a: i64, other: &Value, vm: &VM<'_>) -> Option<bool> {
    match other {
        Value::Int(b) => Some(a == *b),
        Value::Bool(b) => Some(a == i64::from(*b)),
        Value::Float(f) => Some(i64_cmp_f64(a, *f) == Some(Ordering::Equal)),
        Value::InternLongInt(id) => Some(bigint_eq_i64(vm.interns.get_long_int(*id), a)),
        Value::Ref(id) if let HeapData::LongInt(li) = vm.heap.get(*id) => Some(bigint_eq_i64(li.inner(), a)),
        _ => None,
    }
}

/// `f == other`, comparing against ints/bools/big ints *exactly* (no rounding).
pub(crate) fn eq_f64(f: f64, other: &Value, vm: &VM<'_>) -> Option<bool> {
    match other {
        Value::Float(o) => Some(f == *o),
        Value::Int(o) => Some(i64_cmp_f64(*o, f) == Some(Ordering::Equal)),
        Value::Bool(o) => Some(i64_cmp_f64(i64::from(*o), f) == Some(Ordering::Equal)),
        Value::InternLongInt(id) => Some(bigint_eq_f64(vm.interns.get_long_int(*id), f)),
        Value::Ref(id) if let HeapData::LongInt(li) = vm.heap.get(*id) => Some(bigint_eq_f64(li.inner(), f)),
        _ => None,
    }
}

/// `b == other` over the numeric tower, for heap `LongInt` / interned long-int
/// operands. A heap `LongInt` is always outside i64 range, so it never equals
/// an `Int`/`Bool` — but comparing exactly keeps the logic uniform.
pub(crate) fn eq_bigint(b: &BigInt, other: &Value, vm: &VM<'_>) -> Option<bool> {
    match other {
        Value::Int(o) => Some(bigint_eq_i64(b, *o)),
        Value::Bool(o) => Some(bigint_eq_i64(b, i64::from(*o))),
        Value::Float(f) => Some(bigint_eq_f64(b, *f)),
        Value::InternLongInt(id) => Some(b == vm.interns.get_long_int(*id)),
        Value::Ref(id) if let HeapData::LongInt(li) = vm.heap.get(*id) => Some(b == li.inner()),
        _ => None,
    }
}

/// `s == other`, resolving the other operand from an interned or heap string.
pub(crate) fn eq_str(s: &str, other: &Value, vm: &VM<'_>) -> Option<bool> {
    match other {
        Value::InternString(id) => Some(s == vm.interns.get_str(*id)),
        Value::Ref(id) if let HeapData::Str(o) = vm.heap.get(*id) => Some(s == o.as_str()),
        _ => None,
    }
}

/// `b == other`, resolving the other operand from interned or heap bytes.
pub(crate) fn eq_bytes(b: &[u8], other: &Value, vm: &VM<'_>) -> Option<bool> {
    match other {
        Value::InternBytes(id) => Some(b == vm.interns.get_bytes(*id)),
        Value::Ref(id) if let HeapData::Bytes(o) = vm.heap.get(*id) => Some(b == o.as_slice()),
        _ => None,
    }
}

/// Interned or heap-owned string identifier.
///
/// Used when a string value can come from either the intern table (for known
/// static strings and keywords) or from a heap-allocated Python string object.
#[derive(Debug, Clone, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
pub(crate) enum EitherStr {
    /// Interned string identifier (cheap comparisons and no allocation).
    Interned(StringId),
    /// Heap-owned string extracted from a `str` object.
    Heap(String),
}

impl From<StringId> for EitherStr {
    fn from(id: StringId) -> Self {
        Self::Interned(id)
    }
}

impl From<StaticStrings> for EitherStr {
    fn from(s: StaticStrings) -> Self {
        Self::Interned(s.into())
    }
}

/// Convert String to EitherStr: use Interned for known static strings,
/// otherwise use Heap for user-defined field names.
impl From<String> for EitherStr {
    fn from(s: String) -> Self {
        match StaticStrings::from_str(&s) {
            Ok(s) => s.into(),
            Err(_) => Self::Heap(s),
        }
    }
}

impl EitherStr {
    /// Returns the keyword as a str slice for error messages or comparisons.
    pub fn as_str<'a>(&'a self, interns: &'a Interns) -> &'a str {
        match self {
            Self::Interned(id) => interns.get_str(*id),
            Self::Heap(s) => s.as_str(),
        }
    }

    /// Checks whether this keyword matches the given interned identifier.
    pub fn matches(&self, target: StringId, interns: &Interns) -> bool {
        match self {
            Self::Interned(id) => *id == target,
            Self::Heap(s) => s == interns.get_str(target),
        }
    }

    /// Returns the `StringId` if this is an interned attribute.
    #[inline]
    pub fn string_id(&self) -> Option<StringId> {
        match self {
            Self::Interned(id) => Some(*id),
            Self::Heap(_) => None,
        }
    }

    /// Returns the `StaticStrings` if this is an interned attribute from `StaticStrings`s.
    #[inline]
    pub fn static_string(&self) -> Option<StaticStrings> {
        match self {
            Self::Interned(id) => StaticStrings::from_string_id(*id),
            Self::Heap(_) => None,
        }
    }

    /// Converts this `EitherStr` into an owned `String`.
    ///
    /// For interned strings, looks up and clones the string content.
    /// For heap strings, returns the owned string directly.
    pub fn into_string(self, interns: &Interns) -> String {
        match self {
            Self::Interned(id) => interns.get_str(id).to_owned(),
            Self::Heap(s) => s,
        }
    }
}

/// Marker values for special objects that exist but have minimal functionality.
///
/// These are used for:
/// - System objects like `sys.stdout` and `sys.stderr` that need to exist but don't
///   provide functionality in the sandboxed environment
/// - Typing constructs from the `typing` module that are imported for type hints but
///   don't need runtime functionality
///
/// Wraps a `StaticStrings` variant to leverage its string conversion capabilities.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub(crate) struct Marker(pub StaticStrings);

impl Marker {
    /// Returns the Python type of this marker.
    ///
    /// System markers (stdout, stderr) are `TextIOWrapper`.
    /// `typing.Union` has type `type` (matching CPython).
    /// Other typing markers (Any, Optional, etc.) are `_SpecialForm`.
    pub(crate) fn py_type(self) -> Type {
        match self.0 {
            StaticStrings::Stdout | StaticStrings::Stderr => Type::TextIOWrapper,
            StaticStrings::UnionType => Type::Type,
            _ => Type::SpecialForm,
        }
    }

    /// Writes the Python repr for this marker.
    ///
    /// System markers have special repr formats ("<stdout>", "<stderr>").
    /// `typing.Union` uses `<class 'typing.Union'>` format (matching CPython).
    /// Other typing markers are prefixed with "typing." (e.g., "typing.Any").
    pub(crate) fn py_repr_fmt(self, f: &mut impl Write) -> fmt::Result {
        let s: &'static str = self.0.into();
        match self.0 {
            StaticStrings::Stdout => f.write_str("<stdout>")?,
            StaticStrings::Stderr => f.write_str("<stderr>")?,
            StaticStrings::UnionType => f.write_str("<class 'typing.Union'>")?,
            _ => write!(f, "typing.{s}")?,
        }
        Ok(())
    }
}

/// Extracts an immediate integer without promoting it to `BigInt`.
fn immediate_int(value: &Value) -> Option<i64> {
    match value {
        Value::Int(value) => Some(*value),
        Value::Bool(value) => Some(i64::from(*value)),
        _ => None,
    }
}

/// Computes Python-style floor division and modulo.
///
/// Python's division rounds toward negative infinity (floor division),
/// and the remainder has the same sign as the divisor.
/// This differs from Rust's truncating division.
///
/// Returns `None` on overflow (i64::MIN / -1 doesn't fit in i64).
pub(crate) fn floor_divmod(a: i64, b: i64) -> Option<(i64, i64)> {
    let quot = a.checked_div(b)?;
    let rem = a.checked_rem(b)?;

    if rem != 0 && (rem < 0) != (b < 0) {
        Some((quot - 1, rem + b))
    } else {
        Some((quot, rem))
    }
}

/// Computes Python-style float modulo (CPython's `float_rem`).
///
/// Unlike Rust's `%` (which follows the dividend's sign), the result takes the
/// divisor's sign — `-7.0 % 3.0 == 2.0` — and a zero result gets the divisor's
/// sign too (`6.0 % -3.0 == -0.0`). Callers must reject a zero divisor first
/// (`ZeroDivisionError`); this helper assumes `b != 0`.
fn py_float_mod(a: f64, b: f64) -> f64 {
    let r = a % b;
    if r == 0.0 {
        0.0f64.copysign(b)
    } else if (b < 0.0) != (r < 0.0) {
        r + b
    } else {
        r
    }
}

/// Computes the number of significant bits in an i64.
///
/// Returns 0 for 0, otherwise returns ceil(log2(|value|)) + 1 (accounting for sign).
/// For example: 0 -> 0, 1 -> 1, 2 -> 2, 255 -> 8, 256 -> 9.
fn i64_bits(value: i64) -> u64 {
    if value == 0 {
        0
    } else {
        // For negative numbers, use unsigned_abs to get magnitude
        u64::from(64 - value.unsigned_abs().leading_zeros())
    }
}

/// Computes BigInt exponentiation for exponents larger than u32::MAX.
///
/// Uses repeated squaring for efficiency. This is needed when the exponent
/// doesn't fit in a u32, which is required by the `num-bigint` pow method.
fn bigint_pow(base: BigInt, exp: u64) -> BigInt {
    if exp == 0 {
        return BigInt::from(1);
    }
    if exp == 1 {
        return base;
    }

    // Use repeated squaring
    let mut result = BigInt::from(1);
    let mut b = base;
    let mut e = exp;

    while e > 0 {
        if e & 1 == 1 {
            result *= &b;
        }
        b = &b * &b;
        e >>= 1;
    }

    result
}

#[cfg(test)]
mod tests {
    use monty_types::{AssertMessageAnnotations, PrintWriter, ResourceTracker};
    use num_bigint::BigInt;

    use super::*;
    use crate::{heap::HeapReader, intern::InternerBuilder};

    /// Creates a heap and directly allocates a LongInt with the given BigInt value.
    ///
    /// This bypasses `LongInt::into_value()` which would demote i64-fitting values.
    /// Used to test defensive code paths that handle LongInt-as-index scenarios.
    fn create_heap_with_longint(value: BigInt) -> (Heap, HeapId) {
        let heap = Heap::new(16, ResourceTracker::default());
        let long_int = LongInt::new(value);
        let heap_id = heap.allocate(HeapData::LongInt(long_int));
        (heap, heap_id)
    }

    /// Creates a minimal Interns for testing.
    fn create_test_interns() -> Interns {
        let interner = InternerBuilder::new("");
        Interns::new(interner, vec![])
    }

    /// Tests that `as_index()` correctly handles a LongInt containing an i64-fitting value.
    ///
    /// This tests a defensive code path that's normally unreachable because
    /// `LongInt::into_value()` demotes i64-fitting values to `Value::Int`.
    /// However, this path could be reached via deserialization of crafted data.
    #[test]
    fn as_index_longint_fits_in_i64() {
        let (mut heap, heap_id) = create_heap_with_longint(BigInt::from(42));
        let value = Value::Ref(heap_id);

        let mut interns = create_test_interns();
        let result = HeapReader::with(&mut heap, &mut interns, |reader, interns| {
            let vm = VM::new(
                Vec::new(),
                reader,
                interns,
                PrintWriter::Disabled,
                AssertMessageAnnotations::DEFAULT_MAX_BYTES.get(),
            );
            value.as_index(&vm, Type::List)
        });
        assert_eq!(result.unwrap(), 42);
        value.drop_with(&mut heap);
    }

    /// Tests that `as_index()` correctly handles a negative LongInt that fits in i64.
    #[test]
    fn as_index_longint_negative_fits_in_i64() {
        let (mut heap, heap_id) = create_heap_with_longint(BigInt::from(-100));
        let value = Value::Ref(heap_id);

        let mut interns = create_test_interns();
        let result = HeapReader::with(&mut heap, &mut interns, |reader, interns| {
            let vm = VM::new(
                Vec::new(),
                reader,
                interns,
                PrintWriter::Disabled,
                AssertMessageAnnotations::DEFAULT_MAX_BYTES.get(),
            );
            value.as_index(&vm, Type::List)
        });
        assert_eq!(result.unwrap(), -100);
        value.drop_with(&mut heap);
    }

    /// Tests that `as_index()` returns IndexError for LongInt values too large for i64.
    #[test]
    fn as_index_longint_too_large() {
        // 2^100 is way larger than i64::MAX
        let big_value = BigInt::from(2).pow(100);
        let (mut heap, heap_id) = create_heap_with_longint(big_value);
        let value = Value::Ref(heap_id);

        let mut interns = create_test_interns();
        let result = HeapReader::with(&mut heap, &mut interns, |reader, interns| {
            let vm = VM::new(
                Vec::new(),
                reader,
                interns,
                PrintWriter::Disabled,
                AssertMessageAnnotations::DEFAULT_MAX_BYTES.get(),
            );
            value.as_index(&vm, Type::List)
        });
        assert!(result.is_err());
        value.drop_with(&mut heap);
    }

    /// Tests that `as_int()` correctly handles a LongInt containing an i64-fitting value.
    ///
    /// Similar to `as_index`, this tests a defensive code path normally unreachable.
    #[test]
    fn as_int_longint_fits_in_i64() {
        let (mut heap, heap_id) = create_heap_with_longint(BigInt::from(12345));
        let value = Value::Ref(heap_id);

        let mut interns = create_test_interns();
        let result = HeapReader::with(&mut heap, &mut interns, |reader, interns| {
            let vm = VM::new(
                Vec::new(),
                reader,
                interns,
                PrintWriter::Disabled,
                AssertMessageAnnotations::DEFAULT_MAX_BYTES.get(),
            );
            value.as_int(&vm)
        });
        assert_eq!(result.unwrap(), 12345);
        value.drop_with(&mut heap);
    }

    /// Tests that `as_int()` returns an error for LongInt values too large for i64.
    #[test]
    fn as_int_longint_too_large() {
        let big_value = BigInt::from(2).pow(100);
        let (mut heap, heap_id) = create_heap_with_longint(big_value);
        let value = Value::Ref(heap_id);

        let mut interns = create_test_interns();
        let result = HeapReader::with(&mut heap, &mut interns, |reader, interns| {
            let vm = VM::new(
                Vec::new(),
                reader,
                interns,
                PrintWriter::Disabled,
                AssertMessageAnnotations::DEFAULT_MAX_BYTES.get(),
            );
            value.as_int(&vm)
        });
        assert!(result.is_err());
        value.drop_with(&mut heap);
    }

    /// Tests boundary values: i64::MAX as a LongInt.
    #[test]
    fn as_index_longint_at_i64_max() {
        let (mut heap, heap_id) = create_heap_with_longint(BigInt::from(i64::MAX));
        let value = Value::Ref(heap_id);

        let mut interns = create_test_interns();
        let result = HeapReader::with(&mut heap, &mut interns, |reader, interns| {
            let vm = VM::new(
                Vec::new(),
                reader,
                interns,
                PrintWriter::Disabled,
                AssertMessageAnnotations::DEFAULT_MAX_BYTES.get(),
            );
            value.as_index(&vm, Type::List)
        });
        assert_eq!(result.unwrap(), i64::MAX);
        value.drop_with(&mut heap);
    }

    /// Tests boundary values: i64::MIN as a LongInt.
    #[test]
    fn as_index_longint_at_i64_min() {
        let (mut heap, heap_id) = create_heap_with_longint(BigInt::from(i64::MIN));
        let value = Value::Ref(heap_id);

        let mut interns = create_test_interns();
        let result = HeapReader::with(&mut heap, &mut interns, |reader, interns| {
            let vm = VM::new(
                Vec::new(),
                reader,
                interns,
                PrintWriter::Disabled,
                AssertMessageAnnotations::DEFAULT_MAX_BYTES.get(),
            );
            value.as_index(&vm, Type::List)
        });
        assert_eq!(result.unwrap(), i64::MIN);
        value.drop_with(&mut heap);
    }

    /// Tests boundary values: i64::MAX + 1 as a LongInt (should fail).
    #[test]
    fn as_index_longint_just_over_i64_max() {
        let big_value = BigInt::from(i64::MAX) + BigInt::from(1);
        let (mut heap, heap_id) = create_heap_with_longint(big_value);
        let value = Value::Ref(heap_id);

        let mut interns = create_test_interns();
        let result = HeapReader::with(&mut heap, &mut interns, |reader, interns| {
            let vm = VM::new(
                Vec::new(),
                reader,
                interns,
                PrintWriter::Disabled,
                AssertMessageAnnotations::DEFAULT_MAX_BYTES.get(),
            );
            value.as_index(&vm, Type::List)
        });
        assert!(result.is_err());
        value.drop_with(&mut heap);
    }

    /// Tests boundary values: i64::MIN - 1 as a LongInt (should fail).
    #[test]
    fn as_index_longint_just_under_i64_min() {
        let big_value = BigInt::from(i64::MIN) - BigInt::from(1);
        let (mut heap, heap_id) = create_heap_with_longint(big_value);
        let value = Value::Ref(heap_id);

        let mut interns = create_test_interns();
        let result = HeapReader::with(&mut heap, &mut interns, |reader, interns| {
            let vm = VM::new(
                Vec::new(),
                reader,
                interns,
                PrintWriter::Disabled,
                AssertMessageAnnotations::DEFAULT_MAX_BYTES.get(),
            );
            value.as_index(&vm, Type::List)
        });
        assert!(result.is_err());
        value.drop_with(&mut heap);
    }
}