main /Languages/IronPython/IronPython.Modules/cPickle.cs

Language C# Lines 2390
MD5 Hash a3c6fcc07ac2da828908469cc2621697 Estimated Cost $53,476 (why?)
Repository git://github.com/IronLanguages/main.git View Raw File View Project SPDX
   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
/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Apache License, Version 2.0, please send an email to 
 * ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Apache License, Version 2.0.
 *
 * You must not remove this notice, or any other, from this software.
 *
 *
 * ***************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using IronPython.Runtime;
using IronPython.Runtime.Exceptions;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;
using Microsoft.Scripting;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;

#if FEATURE_NUMERICS
using System.Numerics;
#else
using Microsoft.Scripting.Math;
#endif

[assembly: PythonModule("cPickle", typeof(IronPython.Modules.PythonPickle))]
namespace IronPython.Modules {
    public static class PythonPickle {
        public const string __doc__ = "Fast object serialization/deserialization.\n\n"
            + "Differences from CPython:\n"
            + " - does not implement the undocumented fast mode\n";
        [System.Runtime.CompilerServices.SpecialName]
        public static void PerformModuleReload(PythonContext/*!*/ context, PythonDictionary/*!*/ dict) {
            context.EnsureModuleException("PickleError", dict, "PickleError", "cPickle");
            context.EnsureModuleException("PicklingError", dict, "PicklingError", "cPickle");
            context.EnsureModuleException("UnpicklingError", dict, "UnpicklingError", "cPickle");
            context.EnsureModuleException("UnpickleableError", dict, "UnpickleableError", "cPickle");
            context.EnsureModuleException("BadPickleGet", dict, "BadPickleGet", "cPickle");
            dict["__builtins__"] = context.BuiltinModuleInstance;
            dict["compatible_formats"] = PythonOps.MakeList("1.0", "1.1", "1.2", "1.3", "2.0");
        }

        private static readonly PythonStruct.Struct _float64 = PythonStruct.Struct.Create(">d");
        
        private const int highestProtocol = 2;
        
        public const string __version__ = "1.71";
        public const string format_version = "2.0";
        
        public static int HIGHEST_PROTOCOL {
            get { return highestProtocol; }
        }
        
        private const string Newline = "\n";

        #region Public module-level functions

        [Documentation("dump(obj, file, protocol=0) -> None\n\n"
            + "Pickle obj and write the result to file.\n"
            + "\n"
            + "See documentation for Pickler() for a description the file, protocol, and\n"
            + "(deprecated) bin parameters."
            )]
        public static void dump(CodeContext/*!*/ context, object obj, object file, [DefaultParameterValue(null)] object protocol, [DefaultParameterValue(null)] object bin) {
            PicklerObject/*!*/ pickler = new PicklerObject(context, file, protocol, bin);
            pickler.dump(context, obj);
        }

        [Documentation("dumps(obj, protocol=0) -> pickle string\n\n"
            + "Pickle obj and return the result as a string.\n"
            + "\n"
            + "See the documentation for Pickler() for a description of the protocol and\n"
            + "(deprecated) bin parameters."
            )]
        public static string dumps(CodeContext/*!*/ context, object obj, [DefaultParameterValue(null)] object protocol, [DefaultParameterValue(null)] object bin) {
            //??? possible perf enhancement: use a C# TextWriter-backed IFileOutput and
            // thus avoid Python call overhead. Also do similar thing for LoadFromString.
            var stringIO = new StringBuilderOutput();
            PicklerObject/*!*/ pickler = new PicklerObject(context, stringIO, protocol, bin);
            pickler.dump(context, obj);
            return stringIO.GetString();
        }

        [Documentation("load(file) -> unpickled object\n\n"
            + "Read pickle data from the open file object and return the corresponding\n"
            + "unpickled object. Data after the first pickle found is ignored, but the file\n"
            + "cursor is not reset, so if a file objects contains multiple pickles, then\n"
            + "load() may be called multiple times to unpickle them.\n"
            + "\n"
            + "file: an object (such as an open file or a StringIO) with read(num_chars) and\n"
            + "    readline() methods that return strings\n"
            + "\n"
            + "load() automatically determines if the pickle data was written in binary or\n"
            + "text mode."
            )]
        public static object load(CodeContext/*!*/ context, object file) {
            return new UnpicklerObject(context, file).load(context);
        }

        [Documentation("loads(string) -> unpickled object\n\n"
            + "Read a pickle object from a string, unpickle it, and return the resulting\n"
            + "reconstructed object. Characters in the string beyond the end of the first\n"
            + "pickle are ignored."
            )]
        public static object loads(CodeContext/*!*/ context, [BytesConversion]string @string) {
            return new UnpicklerObject(context, new PythonStringInput(@string)).load(context);
        }

        #endregion

        #region File I/O wrappers

        /// <summary>
        /// Interface for "file-like objects" that implement the protocol needed by load() and friends.
        /// This enables the creation of thin wrappers that make fast .NET types and slow Python types look the same.
        /// </summary>
        internal abstract class FileInput {
            public abstract string Read(CodeContext/*!*/ context, int size);
            public abstract string ReadLine(CodeContext/*!*/ context);
            public virtual string ReadLineNoNewLine(CodeContext/*!*/ context) {
                var raw = ReadLine(context);
                return raw.Substring(0, raw.Length - 1);
            }

            public virtual char ReadChar(CodeContext context) {
                string res = Read(context, 1);
                if (res.Length < 1) {
                    throw PythonOps.EofError("unexpected EOF while unpickling");
                }
                return res[0];
            }

            public virtual int ReadInt(CodeContext context) {
                return (int)ReadChar(context) |
                       ((int)ReadChar(context)) << 8 |
                       ((int)ReadChar(context)) << 16 |
                       ((int)ReadChar(context)) << 24;
            }
        }

        /// <summary>
        /// Interface for "file-like objects" that implement the protocol needed by dump() and friends.
        /// This enables the creation of thin wrappers that make fast .NET types and slow Python types look the same.
        /// </summary>
        internal abstract class FileOutput {
            private readonly char[] int32chars = new char[4];

            public abstract void Write(CodeContext/*!*/ context, string data);

            public virtual void Write(CodeContext context, int data) {
                int32chars[0] = (char)(int)((data & 0xff));
                int32chars[1] = (char)(int)((data >> 8) & 0xff);
                int32chars[2] = (char)(int)((data >> 16) & 0xff);
                int32chars[3] = (char)(int)((data >> 24) & 0xff);
                Write(context, new string(int32chars));
            }

            public virtual void Write(CodeContext context, char data) {
                Write(context, ScriptingRuntimeHelpers.CharToString(data));
            }
        }

        private class PythonFileInput : FileInput {
            private object _readMethod;
            private object _readLineMethod;

            public PythonFileInput(CodeContext/*!*/ context, object file) {
                if (!PythonOps.TryGetBoundAttr(context, file, "read", out _readMethod) ||
                    !PythonOps.IsCallable(context, _readMethod) ||
                    !PythonOps.TryGetBoundAttr(context, file, "readline", out _readLineMethod) ||
                    !PythonOps.IsCallable(context, _readLineMethod)
                ) {
                    throw PythonOps.TypeError("argument must have callable 'read' and 'readline' attributes");
                }
            }

            public override string Read(CodeContext/*!*/ context, int size) {
                return Converter.ConvertToString(PythonCalls.Call(context, _readMethod, size));
            }

            public override string ReadLine(CodeContext/*!*/ context) {
                return Converter.ConvertToString(PythonCalls.Call(context, _readLineMethod));
            }
        }

        internal class PythonStringInput : FileInput {
            private readonly string _data;
            int _offset;

            public PythonStringInput(string data) {
                _data = data;
            }

            public override string Read(CodeContext context, int size) {
                var res = _data.Substring(_offset, size);
                _offset += size;
                return res;
            }

            public override string ReadLine(CodeContext context) {
                return ReadLineWorker(true);
            }

            public override string ReadLineNoNewLine(CodeContext context) {
                return ReadLineWorker(false);
            }

            public override char ReadChar(CodeContext context) {
                if (_offset < _data.Length) {
                    return _data[_offset++];
                }
                throw PythonOps.EofError("unexpected EOF while unpickling");
            }

            public override int ReadInt(CodeContext context) {
                if (_offset + 4 <= _data.Length) {
                    int res = _data[_offset]|
                       ((int)_data[_offset  + 1]) << 8 |
                       ((int)_data[_offset + 2]) << 16 |
                       ((int)_data[_offset + 3]) << 24;

                    _offset += 4;
                    return res;
                }

                throw PythonOps.EofError("unexpected EOF while unpickling");
            }

            private string ReadLineWorker(bool includeNewLine) {
                string res;
                for (int i = _offset; i < _data.Length; i++) {
                    if (_data[i] == '\n') {
                        res = _data.Substring(_offset, i - _offset + (includeNewLine ? 1 : 0));
                        _offset = i + 1;
                        return res;
                    }
                }
                res = _data.Substring(_offset);
                _offset = _data.Length;
                return res;
            }
        }

        private class PythonFileLikeOutput : FileOutput {
            private object _writeMethod;

            public PythonFileLikeOutput(CodeContext/*!*/ context, object file) {
                if (!PythonOps.TryGetBoundAttr(context, file, "write", out _writeMethod) ||
                    !PythonOps.IsCallable(context, this._writeMethod)
                ) {
                    throw PythonOps.TypeError("argument must have callable 'write' attribute");
                }
            }

            public override void Write(CodeContext/*!*/ context, string data) {
                PythonCalls.Call(context, _writeMethod, data);
            }
        }

        private class PythonFileOutput : FileOutput {
            private readonly PythonFile _file;

            public PythonFileOutput(PythonFile file) {
                _file = file;
            }

            public override void Write(CodeContext/*!*/ context, string data) {
                _file.write(data);
            }
        }

        private class StringBuilderOutput : FileOutput {
            private readonly StringBuilder _builder = new StringBuilder(4096);

            public string GetString() {
                return _builder.ToString();
            }

            public override void Write(CodeContext context, char data) {
                _builder.Append(data);
            }

            public override void Write(CodeContext context, int data) {
                _builder.Append((char)(int)((data) & 0xff));
                _builder.Append((char)(int)((data >> 8) & 0xff));
                _builder.Append((char)(int)((data >> 16) & 0xff));
                _builder.Append((char)(int)((data >> 24) & 0xff));
            }

            public override void Write(CodeContext context, string data) {
                _builder.Append(data);
            }
        }

        private class PythonReadableFileOutput : PythonFileLikeOutput {
            private object _getValueMethod;

            public PythonReadableFileOutput(CodeContext/*!*/ context, object file)
                : base(context, file) {
                if (!PythonOps.TryGetBoundAttr(context, file, "getvalue", out _getValueMethod) ||
                    !PythonOps.IsCallable(context, _getValueMethod)
                ) {
                    throw PythonOps.TypeError("argument must have callable 'getvalue' attribute");
                }
            }

            public object GetValue(CodeContext/*!*/ context) {
                return PythonCalls.Call(context, _getValueMethod);
            }
        }

        #endregion

        #region Opcode constants

        internal static class Opcode {
            public const char Append = 'a';
            public const char Appends = 'e';
            public const char BinFloat = 'G';
            public const char BinGet = 'h';
            public const char BinInt = 'J';
            public const char BinInt1 = 'K';
            public const char BinInt2 = 'M';
            public const char BinPersid = 'Q';
            public const char BinPut = 'q';
            public const char BinString = 'T';
            public const char BinUnicode = 'X';
            public const char Build = 'b';
            public const char Dict = 'd';
            public const char Dup = '2';
            public const char EmptyDict = '}';
            public const char EmptyList = ']';
            public const char EmptyTuple = ')';
            public const char Ext1 = '\x82';
            public const char Ext2 = '\x83';
            public const char Ext4 = '\x84';
            public const char Float = 'F';
            public const char Get = 'g';
            public const char Global = 'c';
            public const char Inst = 'i';
            public const char Int = 'I';
            public const char List = 'l';
            public const char Long = 'L';
            public const char Long1 = '\x8a';
            public const char Long4 = '\x8b';
            public const char LongBinGet = 'j';
            public const char LongBinPut = 'r';
            public const char Mark = '(';
            public const char NewFalse = '\x89';
            public const char NewObj = '\x81';
            public const char NewTrue = '\x88';
            public const char NoneValue = 'N';
            public const char Obj = 'o';
            public const char PersId = 'P';
            public const char Pop = '0';
            public const char PopMark = '1';
            public const char Proto = '\x80';
            public const char Put = 'p';
            public const char Reduce = 'R';
            public const char SetItem = 's';
            public const char SetItems = 'u';
            public const char ShortBinstring = 'U';
            public const char Stop = '.';
            public const char String = 'S';
            public const char Tuple = 't';
            public const char Tuple1 = '\x85';
            public const char Tuple2 = '\x86';
            public const char Tuple3 = '\x87';
            public const char Unicode = 'V';
        }

        #endregion

        #region Pickler object

        public static PicklerObject/*!*/ Pickler(CodeContext/*!*/ context, [DefaultParameterValue(null)]object file, [DefaultParameterValue(null)]object protocol, [DefaultParameterValue(null)]object bin) {
            return new PicklerObject(context, file, protocol, bin);
        }

        [Documentation("Pickler(file, protocol=0) -> Pickler object\n\n"
            + "A Pickler object serializes Python objects to a pickle bytecode stream, which\n"
            + "can then be converted back into equivalent objects using an Unpickler.\n"
            + "\n"
            + "file: an object (such as an open file) that has a write(string) method.\n"
            + "protocol: if omitted, protocol 0 is used. If HIGHEST_PROTOCOL or a negative\n"
            + "    number, the highest available protocol is used.\n"
            + "bin: (deprecated; use protocol instead) for backwards compability, a 'bin'\n"
            + "    keyword parameter is supported. When protocol is specified it is ignored.\n"
            + "    If protocol is not specified, then protocol 0 is used if bin is false, and\n"
            + "    protocol 1 is used if bin is true."
            )]
        [PythonType("Pickler"), PythonHidden]
        public class PicklerObject {

            private const char LowestPrintableChar = (char)32;
            private const char HighestPrintableChar = (char)126;
            // max elements that can be set/appended at a time using SETITEMS/APPENDS

            private delegate void PickleFunction(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object value);
            private static readonly Dictionary<Type, PickleFunction> _dispatchTable;

            private int _batchSize = 1000;
            private FileOutput _file;
            private int _protocol;
            private PythonDictionary _memo;               // memo if the user accesses the memo property
            private Dictionary<object, int> _privMemo;    // internal fast memo which we can use if the user doesn't access memo
            private object _persist_id;

            static PicklerObject() {
                _dispatchTable = new Dictionary<Type, PickleFunction>();
                _dispatchTable[typeof(PythonDictionary)] = SaveDict;
                _dispatchTable[typeof(PythonTuple)] = SaveTuple;
                _dispatchTable[typeof(List)] = SaveList;
                _dispatchTable[typeof(OldClass)] = SaveGlobal;
                _dispatchTable[typeof(PythonFunction)] = SaveGlobal;
                _dispatchTable[typeof(BuiltinFunction)] = SaveGlobal;
                _dispatchTable[typeof(PythonType)] = SaveGlobal;
                _dispatchTable[typeof(OldInstance)] = SaveInstance;
            }
            #region Public API

            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
            public PythonDictionary memo {
                get {
                    if (_memo == null) {
                        // create publicly viewable memo
                        PythonDictionary resMemo = new PythonDictionary();
                        foreach (var v in _privMemo) {
                            resMemo._storage.AddNoLock(
                                ref resMemo._storage,
                                Builtin.id(v.Key),
                                PythonTuple.MakeTuple(v.Value, v.Key)
                            );
                        }
                        _memo = resMemo;
                    }
                    return _memo;
                }
                set {
                    _memo = value;
                    _privMemo = null;
                }
            }

            public int proto {
                get { return _protocol; }
                set { _protocol = value; }
            }

            public int _BATCHSIZE {
                get { return _batchSize; }
                set { _batchSize = value; }
            }

            public object persistent_id {
                get {
                    return _persist_id;
                }
                set {
                    _persist_id = value;
                }
            }

            public int binary {
                get { return _protocol == 0 ? 1 : 0; }
                set { _protocol = value; }
            }

            public int fast {
                // We don't implement fast, but we silently ignore it when it's set so that test_cpickle works.
                // For a description of fast, see http://mail.python.org/pipermail/python-bugs-list/2001-October/007695.html
                get { return 0; }
                set { /* ignore */ }
            }

            public PicklerObject(CodeContext/*!*/ context, object file, object protocol, object bin) {
                int intProtocol;
                if (file == null) {
                    _file = new PythonReadableFileOutput(context, new PythonStringIO.StringO());
                } else if (Converter.TryConvertToInt32(file, out intProtocol)) {
                    // For undocumented (yet tested in official CPython tests) list-based pickler, the
                    // user could do something like Pickler(1), which would create a protocol-1 pickler
                    // with an internal string output buffer (retrievable using getvalue()). For a little
                    // more info, see
                    // https://sourceforge.net/tracker/?func=detail&atid=105470&aid=939395&group_id=5470
                    _file = new PythonReadableFileOutput(context, new PythonStringIO.StringO());
                    protocol = file;
                } else if (file is PythonFile) {
                    _file = new PythonFileOutput((PythonFile)file);
                } else if (file is FileOutput) {
                    _file = (FileOutput)file;
                } else {
                    _file = new PythonFileLikeOutput(context, file);
                }

                _privMemo = new Dictionary<object, int>(256, ReferenceEqualityComparer.Instance);

                if (protocol == null) protocol = PythonOps.IsTrue(bin) ? 1 : 0;

                intProtocol = context.LanguageContext.ConvertToInt32(protocol);
                if (intProtocol > highestProtocol) {
                    throw PythonOps.ValueError("pickle protocol {0} asked for; the highest available protocol is {1}", intProtocol, highestProtocol);
                } else if (intProtocol < 0) {
                    this._protocol = highestProtocol;
                } else {
                    this._protocol = intProtocol;
                }
            }

            [Documentation("dump(obj) -> None\n\n"
                + "Pickle obj and write the result to the file object that was passed to the\n"
                + "constructor\n."
                + "\n"
                + "Note that you may call dump() multiple times to pickle multiple objects. To\n"
                + "unpickle the stream, you will need to call Unpickler's load() method a\n"
                + "corresponding number of times.\n"
                + "\n"
                + "The first time a particular object is encountered, it will be pickled normally.\n"
                + "If the object is encountered again (in the same or a later dump() call), a\n"
                + "reference to the previously generated value will be pickled. Unpickling will\n"
                + "then create multiple references to a single object."
                )]
            public void dump(CodeContext/*!*/ context, object obj) {
                if (_protocol >= 2) WriteProto(context);
                Save(context, obj);
                Write(context, Opcode.Stop);
            }

            [Documentation("clear_memo() -> None\n\n"
                + "Clear the memo, which is used internally by the pickler to keep track of which\n"
                + "objects have already been pickled (so that shared or recursive objects are\n"
                + "pickled only once)."
                )]
            public void clear_memo() {
                if (_memo != null) {
                    _memo.Clear();
                } else {
                    _privMemo.Clear();
                }
            }

            private void Memoize(object obj) {
                if (_memo != null) {
                    if (!MemoContains(PythonOps.Id(obj))) {
                        _memo[PythonOps.Id(obj)] = PythonTuple.MakeTuple(_memo.Count, obj);
                    }
                } else {
                    if(!_privMemo.ContainsKey(obj)) {
                        _privMemo[obj] = _privMemo.Count;
                    }
                }
            }

            private int MemoizeNew(object obj) {
                
                int res;
                if (_memo != null) {
                    Debug.Assert(!_memo.ContainsKey(obj));
                    _memo[PythonOps.Id(obj)] = PythonTuple.MakeTuple(res = _memo.Count, obj);
                } else {
                    Debug.Assert(!_privMemo.ContainsKey(obj));
                    _privMemo[obj] = res = _privMemo.Count;
                }
                return res;
            }

            private bool MemoContains(object obj) {                
                if (_memo != null) {
                    return _memo.Contains(PythonOps.Id(obj));
                }

                return _privMemo.ContainsKey(obj);
            }

            private bool TryWriteFastGet(CodeContext context, object obj) {
                int value;
                if (_memo != null) {
                    return TryWriteSlowGet(context, obj);
                } else if (_privMemo.TryGetValue(obj, out value)) {
                    WriteGetOrPut(context, true, value);
                    return true;
                }
                return false;
            }

            private bool TryWriteSlowGet(CodeContext context, object obj) {
                object value;
                if (_memo.TryGetValue(obj, out value)) {
                    WriteGetOrPut(context, true, (PythonTuple)value);
                    return true;
                }
                return false;
            }

            [Documentation("getvalue() -> string\n\n"
                + "Return the value of the internal string. Raises PicklingError if a file object\n"
                + "was passed to this pickler's constructor."
                )]
            public object getvalue(CodeContext/*!*/ context) {
                if (_file is PythonReadableFileOutput) {
                    return ((PythonReadableFileOutput)_file).GetValue(context);
                }
                throw PythonExceptions.CreateThrowable(PicklingError(context), "Attempt to getvalue() a non-list-based pickler");
            }

            #endregion

            #region Save functions

            private void Save(CodeContext/*!*/ context, object obj) {
                if (_persist_id == null || !TrySavePersistId(context, obj)) {
                    PickleFunction pickleFunction;
                    // several typees are never memoized, check for these first.
                    if (obj == null) {
                        SaveNone(this, context, obj);
                    } else if (obj is int) {
                        SaveInteger(this, context, obj);
                    } else if(obj is BigInteger) {
                        SaveLong(this, context, obj);
                    } else if (obj is bool) {
                        SaveBoolean(this, context, obj);
                    } else if (obj is double) {
                        SaveFloat(this, context, obj);
                    } else if(!TryWriteFastGet(context, obj)) {
                        if (obj is string) {
                            // strings are common, specialize them.
                            SaveUnicode(this, context, obj);
                        } else {
                            if (!_dispatchTable.TryGetValue(obj.GetType(), out pickleFunction)) {
                                if (obj is PythonType) {
                                    // treat classes with metaclasses like regular classes
                                    pickleFunction = SaveGlobal;
                                } else {
                                    pickleFunction = SaveObject;
                                }
                            }
                            pickleFunction(this, context, obj);
                        }      
                    }
                }
            }

            private bool TrySavePersistId(CodeContext context, object obj) {
                Debug.Assert(_persist_id != null);
                string res = Converter.ConvertToString(PythonContext.GetContext(context).CallSplat(_persist_id, obj));
                if (res != null) {
                    SavePersId(context, res);
                    return true;
                }
                
                return false;
            }

            private void SavePersId(CodeContext/*!*/ context, string res) {
                if (this.binary != 0) {
                    Save(context, res);
                    Write(context, Opcode.BinPersid);
                } else {
                    Write(context, Opcode.PersId);
                    Write(context, res);
                    Write(context, "\n");
                }
            }

            private static void SaveBoolean(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
                Debug.Assert(DynamicHelpers.GetPythonType(obj).Equals(TypeCache.Boolean), "arg must be bool");
                if (pickler._protocol < 2) {
                    pickler.Write(context, Opcode.Int);
                    pickler.Write(context, String.Format("0{0}", ((bool)obj) ? 1 : 0));
                    pickler.Write(context, Newline);
                } else {
                    if ((bool)obj) {
                        pickler.Write(context, Opcode.NewTrue);
                    } else {
                        pickler.Write(context, Opcode.NewFalse);
                    }
                }
            }

            private static void SaveDict(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
                Debug.Assert(DynamicHelpers.GetPythonType(obj).Equals(TypeCache.Dict), "arg must be dict");
                Debug.Assert(!pickler.MemoContains(obj));

                int index = pickler.MemoizeNew(obj);

                if (pickler._protocol < 1) {
                    pickler.Write(context, Opcode.Mark);
                    pickler.Write(context, Opcode.Dict);
                } else {
                    pickler.Write(context, Opcode.EmptyDict);
                }

                pickler.WritePut(context, index);

                pickler.BatchSetItems(context, (PythonDictionary)obj);
            }

            private static void SaveFloat(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
                Debug.Assert(DynamicHelpers.GetPythonType(obj).Equals(TypeCache.Double), "arg must be float");

                if (pickler._protocol < 1) {
                    pickler.Write(context, Opcode.Float);
                    pickler.WriteFloatAsString(context, obj);
                } else {
                    pickler.Write(context, Opcode.BinFloat);
                    pickler.WriteFloat64(context, obj);
                }
            }

            private static void SaveGlobal(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
                Debug.Assert(
                    DynamicHelpers.GetPythonType(obj).Equals(TypeCache.OldClass) ||
                    DynamicHelpers.GetPythonType(obj).Equals(TypeCache.Function) ||
                    DynamicHelpers.GetPythonType(obj).Equals(TypeCache.BuiltinFunction) ||
                    DynamicHelpers.GetPythonType(obj).Equals(TypeCache.PythonType) ||
                    DynamicHelpers.GetPythonType(obj).IsSubclassOf(TypeCache.PythonType),
                    "arg must be classic class, function, built-in function or method, or new-style type"
                );

                PythonType pt = obj as PythonType;
                if (pt != null) {
                    pickler.SaveGlobalByName(context, obj, pt.Name);
                } else {
                    object name;
                    if (PythonOps.TryGetBoundAttr(context, obj, "__name__", out name)) {
                        pickler.SaveGlobalByName(context, obj, name);
                    } else {
                        throw pickler.CannotPickle(context, obj, "could not determine its __name__");
                    }
                }
            }

            private void SaveGlobalByName(CodeContext/*!*/ context, object obj, object name) {
                Debug.Assert(!MemoContains(obj));

                object moduleName = FindModuleForGlobal(context, obj, name);

                if (_protocol >= 2) {
                    object code;
                    if (PythonCopyReg.GetExtensionRegistry(context).TryGetValue(PythonTuple.MakeTuple(moduleName, name), out code)) {
                        if (IsUInt8(context, code)) {
                            Write(context, Opcode.Ext1);
                            WriteUInt8(context, code);
                        } else if (IsUInt16(context, code)) {
                            Write(context, Opcode.Ext2);
                            WriteUInt16(context, code);
                        } else if (IsInt32(context, code)) {
                            Write(context, Opcode.Ext4);
                            WriteInt32(context, code);
                        } else {
                            throw PythonOps.RuntimeError("unrecognized integer format");
                        }
                        return;
                    }
                }

                MemoizeNew(obj);

                Write(context, Opcode.Global);
                WriteStringPair(context, moduleName, name);
                WritePut(context, obj);
            }

            private static void SaveInstance(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
                Debug.Assert(DynamicHelpers.GetPythonType(obj).Equals(TypeCache.OldInstance), "arg must be old-class instance");
                Debug.Assert(!pickler.MemoContains(obj));

                pickler.Write(context, Opcode.Mark);

                // Memoize() call isn't in the usual spot to allow class to be memoized before
                // instance (when using proto other than 0) to match CPython's bytecode output

                object objClass;
                if (!PythonOps.TryGetBoundAttr(context, obj, "__class__", out objClass)) {
                    throw pickler.CannotPickle(context, obj, "could not determine its __class__");
                }

                if (pickler._protocol < 1) {
                    object className, classModuleName;
                    if (!PythonOps.TryGetBoundAttr(context, objClass, "__name__", out className)) {
                        throw pickler.CannotPickle(context, obj, "its __class__ has no __name__");
                    }
                    classModuleName = pickler.FindModuleForGlobal(context, objClass, className);

                    Debug.Assert(!pickler.MemoContains(obj));
                    pickler.MemoizeNew(obj);
                    pickler.WriteInitArgs(context, obj);
                    pickler.Write(context, Opcode.Inst);
                    pickler.WriteStringPair(context, classModuleName, className);
                } else {
                    pickler.Save(context, objClass);
                    pickler.Memoize(obj);
                    pickler.WriteInitArgs(context, obj);
                    pickler.Write(context, Opcode.Obj);
                }

                pickler.WritePut(context, obj);

                object getStateCallable;
                if (PythonOps.TryGetBoundAttr(context, obj, "__getstate__", out getStateCallable)) {
                    pickler.Save(context, PythonCalls.Call(context, getStateCallable));
                } else {
                    pickler.Save(context, PythonOps.GetBoundAttr(context, obj, "__dict__"));
                }

                pickler.Write(context, Opcode.Build);
            }

            private static void SaveInteger(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
                Debug.Assert(DynamicHelpers.GetPythonType(obj).Equals(TypeCache.Int32), "arg must be int");
                if (pickler._protocol < 1) {
                    pickler.Write(context, Opcode.Int);
                    pickler.WriteIntAsString(context, obj);
                } else {
                    if (IsUInt8(context, obj)) {
                        pickler.Write(context, Opcode.BinInt1);
                        pickler.WriteUInt8(context, obj);
                    } else if (IsUInt16(context, obj)) {
                        pickler.Write(context, Opcode.BinInt2);
                        pickler.WriteUInt16(context, obj);
                    } else if (IsInt32(context, obj)) {
                        pickler.Write(context, Opcode.BinInt);
                        pickler.WriteInt32(context, obj);
                    } else {
                        throw PythonOps.RuntimeError("unrecognized integer format");
                    }
                }
            }

            private static void SaveList(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
                Debug.Assert(DynamicHelpers.GetPythonType(obj).Equals(TypeCache.List), "arg must be list");
                Debug.Assert(!pickler.MemoContains(obj));

                int index = pickler.MemoizeNew(obj);
                if (pickler._protocol < 1) {
                    pickler.Write(context, Opcode.Mark);
                    pickler.Write(context, Opcode.List);
                } else {
                    pickler.Write(context, Opcode.EmptyList);
                }

                pickler.WritePut(context, index);
                pickler.BatchAppends(context, ((IEnumerable)obj).GetEnumerator());
            }
#if CLR2
            private static readonly BigInteger MaxInt = BigInteger.Create(Int32.MaxValue);
            private static readonly BigInteger MinInt = BigInteger.Create(Int32.MinValue);
#else
            private static readonly BigInteger MaxInt = new BigInteger(Int32.MaxValue);
            private static readonly BigInteger MinInt = new BigInteger(Int32.MinValue);
#endif
            private static void SaveLong(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
                Debug.Assert(DynamicHelpers.GetPythonType(obj).Equals(TypeCache.BigInteger), "arg must be long");

                BigInteger bi = (BigInteger)obj;
                if (pickler._protocol < 2) {
                    pickler.Write(context, Opcode.Long);
                    pickler.WriteLongAsString(context, obj);
                } else if (bi.IsZero()) {
                    pickler.Write(context, Opcode.Long1);
                    pickler.WriteUInt8(context, 0);
                } else if (bi <= MaxInt && bi >= MinInt) {
                    pickler.Write(context, Opcode.Long1);
                    int value = (int)bi;
                    if (IsInt8(value)) {
                        pickler.WriteUInt8(context, 1);
                        pickler._file.Write(context, (char)(byte)value);
                    } else if (IsInt16(value)) {
                        pickler.WriteUInt8(context, 2);
                        pickler.WriteUInt8(context, value & 0xff);
                        pickler.WriteUInt8(context, (value >> 8) & 0xff);
                    } else {
                        pickler.WriteUInt8(context, 4);
                        pickler.WriteInt32(context, value);
                    }
                } else {
                    byte[] dataBytes = bi.ToByteArray();
                    if (dataBytes.Length < 256) {
                        pickler.Write(context, Opcode.Long1);
                        pickler.WriteUInt8(context, dataBytes.Length);
                    } else {
                        pickler.Write(context, Opcode.Long4);
                        pickler.WriteInt32(context, dataBytes.Length);
                    }

                    foreach (byte b in dataBytes) {
                        pickler.WriteUInt8(context, b);
                    }
                }
            }

            private static void SaveNone(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
                Debug.Assert(DynamicHelpers.GetPythonType(obj).Equals(TypeCache.Null), "arg must be None");
                pickler.Write(context, Opcode.NoneValue);
            }

            /// <summary>
            /// Call the appropriate reduce method for obj and pickle the object using
            /// the resulting data. Use the first available of
            /// copy_reg.dispatch_table[type(obj)], obj.__reduce_ex__, and obj.__reduce__.
            /// </summary>
            private void SaveObject(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
                Debug.Assert(!MemoContains(obj));
                MemoizeNew(obj);

                object reduceCallable, result;
                PythonType objType = DynamicHelpers.GetPythonType(obj);

                if (((IDictionary<object, object>)PythonCopyReg.GetDispatchTable(context)).TryGetValue(objType, out reduceCallable)) {
                    result = PythonCalls.Call(context, reduceCallable, obj);
                } else if (PythonOps.TryGetBoundAttr(context, obj, "__reduce_ex__", out reduceCallable)) {
                    if (obj is PythonType) {
                        result = context.LanguageContext.Call(context, reduceCallable, obj, _protocol);
                    } else {
                        result = context.LanguageContext.Call(context, reduceCallable, _protocol);
                    }
                } else if (PythonOps.TryGetBoundAttr(context, obj, "__reduce__", out reduceCallable)) {
                    if (obj is PythonType) {
                        result = context.LanguageContext.Call(context, reduceCallable, obj);
                    } else {
                        result = context.LanguageContext.Call(context, reduceCallable);
                    }
                } else {
                    throw PythonOps.AttributeError("no reduce function found for {0}", obj);
                }

                if (objType.Equals(TypeCache.String)) {
                    if (!TryWriteFastGet(context, obj)) {
                        SaveGlobalByName(context, obj, result);
                    }
                } else if (result is PythonTuple) {
                    PythonTuple rt = (PythonTuple)result;
                    switch (rt.__len__()) {
                        case 2:
                            SaveReduce(context, obj, reduceCallable, rt[0], rt[1], null, null, null);
                            break;
                        case 3:
                            SaveReduce(context, obj, reduceCallable, rt[0], rt[1], rt[2], null, null);
                            break;
                        case 4:
                            SaveReduce(context, obj, reduceCallable, rt[0], rt[1], rt[2], rt[3], null);
                            break;
                        case 5:
                            SaveReduce(context, obj, reduceCallable, rt[0], rt[1], rt[2], rt[3], rt[4]);
                            break;
                        default:
                            throw CannotPickle(context, obj, "tuple returned by {0} must have to to five elements", reduceCallable);
                    }
                } else {
                    throw CannotPickle(context, obj, "{0} must return string or tuple", reduceCallable);
                }
            }

            /// <summary>
            /// Pickle the result of a reduce function.
            /// 
            /// Only context, obj, func, and reduceCallable are required; all other arguments may be null.
            /// </summary>
            private void SaveReduce(CodeContext/*!*/ context, object obj, object reduceCallable, object func, object args, object state, object listItems, object dictItems) {
                if (!PythonOps.IsCallable(context, func)) {
                    throw CannotPickle(context, obj, "func from reduce() should be callable");
                } else if (!(args is PythonTuple) && args != null) {
                    throw CannotPickle(context, obj, "args from reduce() should be a tuple");
                } else if (listItems != null && !(listItems is IEnumerator)) {
                    throw CannotPickle(context, obj, "listitems from reduce() should be a list iterator");
                } else if (dictItems != null && !(dictItems is IEnumerator)) {
                    throw CannotPickle(context, obj, "dictitems from reduce() should be a dict iterator");
                }

                object funcName;
                string funcNameString;
                if (func is PythonType) {
                    funcNameString = ((PythonType)func).Name;
                } else {
                    if (!PythonOps.TryGetBoundAttr(context, func, "__name__", out funcName)) {
                        throw CannotPickle(context, obj, "func from reduce() ({0}) should have a __name__ attribute");
                    } else if (!Converter.TryConvertToString(funcName, out funcNameString) || funcNameString == null) {
                        throw CannotPickle(context, obj, "__name__ of func from reduce() must be string");
                    }
                }

                if (_protocol >= 2 && "__newobj__" == funcNameString) {
                    if (args == null) {
                        throw CannotPickle(context, obj, "__newobj__ arglist is None");
                    }
                    PythonTuple argsTuple = (PythonTuple)args;
                    if (argsTuple.__len__() == 0) {
                        throw CannotPickle(context, obj, "__newobj__ arglist is empty");
                    } else if (!DynamicHelpers.GetPythonType(obj).Equals(argsTuple[0])) {
                        throw CannotPickle(context, obj, "args[0] from __newobj__ args has the wrong class");
                    }
                    Save(context, argsTuple[0]);
                    Save(context, argsTuple[new Slice(1, null)]);
                    Write(context, Opcode.NewObj);
                } else {
                    Save(context, func);
                    Save(context, args);
                    Write(context, Opcode.Reduce);
                }

                WritePut(context, obj);

                if (state != null) {
                    Save(context, state);
                    Write(context, Opcode.Build);
                }

                if (listItems != null) {
                    BatchAppends(context, (IEnumerator)listItems);
                }

                if (dictItems != null) {
                    BatchSetItems(context, (IEnumerator)dictItems);
                }
            }

            private static void SaveTuple(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
                Debug.Assert(DynamicHelpers.GetPythonType(obj).Equals(TypeCache.PythonTuple), "arg must be tuple");
                Debug.Assert(!pickler.MemoContains(obj));
                PythonTuple t = (PythonTuple)obj;
                char opcode;
                bool needMark = false;
                int len = t._data.Length;
                if (pickler._protocol > 0 && len == 0) {
                    opcode = Opcode.EmptyTuple;
                } else if (pickler._protocol >= 2 && len == 1) {
                    opcode = Opcode.Tuple1;
                } else if (pickler._protocol >= 2 && len == 2) {
                    opcode = Opcode.Tuple2;
                } else if (pickler._protocol >= 2 && len == 3) {
                    opcode = Opcode.Tuple3;
                } else {
                    opcode = Opcode.Tuple;
                    needMark = true;
                }

                if (needMark) pickler.Write(context, Opcode.Mark);
                var data = t._data;
                for (int i = 0; i < data.Length; i++) {
                    pickler.Save(context, data[i]);
                }

                if (len > 0) {
                    if (pickler.MemoContains(obj)) {
                        // recursive tuple
                        if (pickler._protocol == 1) {
                            pickler.Write(context, Opcode.PopMark);
                        } else {
                            if (pickler._protocol == 0) {
                                pickler.Write(context, Opcode.Pop);
                            }
                            for (int i = 0; i < len; i++) {
                                pickler.Write(context, Opcode.Pop);
                            }
                        }
                        pickler.WriteGet(context, obj);
                        return;
                    }

                    pickler.Write(context, opcode);

                    pickler.Memoize(t);
                    pickler.WritePut(context, t);
                } else {
                    pickler.Write(context, opcode);
                }
            }

            private static void SaveUnicode(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
                Debug.Assert(DynamicHelpers.GetPythonType(obj).Equals(TypeCache.String), "arg must be unicode");
                Debug.Assert(!pickler.MemoContains(obj));

                if (pickler._memo != null) {
                    pickler.MemoizeNew(obj);
                    if (pickler._protocol < 1) {
                        pickler.Write(context, Opcode.Unicode);
                        pickler.WriteUnicodeStringRaw(context, obj);
                    } else {
                        pickler.Write(context, Opcode.BinUnicode);
                        pickler.WriteUnicodeStringUtf8(context, obj);
                    }

                    pickler.WritePut(context, obj);
                } else {
                    var memo = pickler._privMemo[obj] = pickler._privMemo.Count;

                    if (pickler._protocol < 1) {
                        pickler.Write(context, Opcode.Unicode);
                        pickler.WriteUnicodeStringRaw(context, obj);
                    } else {
                        pickler.Write(context, Opcode.BinUnicode);
                        pickler.WriteUnicodeStringUtf8(context, obj);
                    }

                    pickler.WriteGetOrPut(context, false, memo);
                }

            }

            #endregion

            #region Output encoding

            /// <summary>
            /// Write value in pickle decimalnl_short format.
            /// </summary>
            private void WriteFloatAsString(CodeContext/*!*/ context, object value) {                
                Debug.Assert(DynamicHelpers.GetPythonType(value).Equals(TypeCache.Double));
                Write(context, DoubleOps.__repr__(context, (double)value));
                Write(context, Newline);
            }

            /// <summary>
            /// Write value in pickle float8 format.
            /// </summary>
            private void WriteFloat64(CodeContext/*!*/ context, object value) {
                Debug.Assert(DynamicHelpers.GetPythonType(value).Equals(TypeCache.Double));
                Write(context, _float64.pack(context, value));
            }

            /// <summary>
            /// Write value in pickle uint1 format.
            /// </summary>
            private void WriteUInt8(CodeContext/*!*/ context, object value) {
                Debug.Assert(IsUInt8(context, value));

                if (value is int) {
                    Write(context, ScriptingRuntimeHelpers.CharToString((char)(int)(value)));
                } else if (value is BigInteger) {
                    Write(context, ScriptingRuntimeHelpers.CharToString((char)(int)(BigInteger)(value)));
                } else if (value is byte) {
                    // TODO: Shouldn't be here
                    Write(context, ScriptingRuntimeHelpers.CharToString((char)(byte)(value)));
                } else {
                    throw Assert.Unreachable;
                }
            }

            private void WriteUInt8(CodeContext/*!*/ context, int value) {
                _file.Write(context, (char)value);
            }

            /// <summary>
            /// Write value in pickle uint2 format.
            /// </summary>
            private void WriteUInt16(CodeContext/*!*/ context, object value) {
                Debug.Assert(IsUInt16(context, value));
                int iVal = (int)value;
                WriteUInt8(context, iVal & 0xff);
                WriteUInt8(context, (iVal >> 8) & 0xff);
            }

            /// <summary>
            /// Write value in pickle int4 format.
            /// </summary>
            private void WriteInt32(CodeContext/*!*/ context, object value) {
                Debug.Assert(IsInt32(context, value));

                int val = (int)value;
                WriteInt32(context, val);
            }

            private void WriteInt32(CodeContext context, int val) {
                _file.Write(context, val);
            }

            /// <summary>
            /// Write value in pickle decimalnl_short format.
            /// </summary>
            private void WriteIntAsString(CodeContext/*!*/ context, object value) {
                Debug.Assert(IsInt32(context, value));
                Write(context, PythonOps.Repr(context, value));
                Write(context, Newline);
            }

            /// <summary>
            /// Write value in pickle decimalnl_short format.
            /// </summary>
            private void WriteIntAsString(CodeContext/*!*/ context, int value) {
                Write(context, value.ToString());
                Write(context, Newline);
            }

            /// <summary>
            /// Write value in pickle decimalnl_long format.
            /// </summary>
            private void WriteLongAsString(CodeContext/*!*/ context, object value) {
                Debug.Assert(DynamicHelpers.GetPythonType(value).Equals(TypeCache.BigInteger));
                Write(context, PythonOps.Repr(context, value));
                Write(context, Newline);
            }

            /// <summary>
            /// Write value in pickle unicodestringnl format.
            /// </summary>
            private void WriteUnicodeStringRaw(CodeContext/*!*/ context, object value) {
                Debug.Assert(DynamicHelpers.GetPythonType(value).Equals(TypeCache.String));
                // manually escape backslash and newline
                Write(context, StringOps.RawUnicodeEscapeEncode(((string)value).Replace("\\", "\\u005c").Replace("\n", "\\u000a")));
                Write(context, Newline);
            }

            /// <summary>
            /// Write value in pickle unicodestring4 format.
            /// </summary>
            private void WriteUnicodeStringUtf8(CodeContext/*!*/ context, object value) {
                Debug.Assert(DynamicHelpers.GetPythonType(value).Equals(TypeCache.String));
                string strVal = (string)value;

                // if the string contains non-ASCII elements it needs to be re-encoded as UTF8.
                for (int i = 0; i < strVal.Length; i++) {                    
                    if (strVal[i] >= 128) {
                        string encodedString = System.Text.Encoding.UTF8.GetBytes((string)value).MakeString();
                        WriteInt32(context, encodedString.Length);
                        Write(context, encodedString);
                        return;
                    }
                }

                WriteInt32(context, strVal.Length);
                Write(context, strVal);
            }

            /// <summary>
            /// Write value in pickle stringnl_noescape_pair format.
            /// </summary>
            private void WriteStringPair(CodeContext/*!*/ context, object value1, object value2) {
                Debug.Assert(DynamicHelpers.GetPythonType(value1).Equals(TypeCache.String));
                Debug.Assert(DynamicHelpers.GetPythonType(value2).Equals(TypeCache.String));
#if DEBUG
                Debug.Assert(IsPrintableAscii(value1));
                Debug.Assert(IsPrintableAscii(value2));
#endif
                Write(context, (string)value1);
                Write(context, Newline);
                Write(context, (string)value2);
                Write(context, Newline);
            }

            #endregion

            #region Type checking

            /// <summary>
            /// Return true if value is appropriate for formatting in pickle uint1 format.
            /// </summary>
            private static bool IsUInt8(CodeContext/*!*/ context, object value) {
                if (value is int) {
                    return IsUInt8((int)value);
                }
                PythonContext pc = PythonContext.GetContext(context);

                return pc.LessThanOrEqual(0, value) && pc.LessThan(value, 1 << 8);
            }

            private static bool IsUInt8(int value) {
                return (value >= 0 && value < 1 << 8);
            }

            private static bool IsInt8(int value) {
                return (value >= SByte.MinValue && value <= SByte.MaxValue);
            }

            /// <summary>
            /// Return true if value is appropriate for formatting in pickle uint2 format.
            /// </summary>
            private static bool IsUInt16(CodeContext/*!*/ context, object value) {
                if (value is int) {
                    return IsUInt16((int)value);
                }
                PythonContext pc = PythonContext.GetContext(context);

                return pc.LessThanOrEqual(1 << 8, value) && pc.LessThan(value, 1 << 16);
            }

            private static bool IsUInt16(int value) {
                return (value >= 0 && value < 1 << 16);
            }

            private static bool IsInt16(int value) {
                return (value >= short.MinValue && value <= short.MaxValue);
            }

            /// <summary>
            /// Return true if value is appropriate for formatting in pickle int4 format.
            /// </summary>
            private static bool IsInt32(CodeContext/*!*/ context, object value) {
                PythonContext pc = PythonContext.GetContext(context);

                return pc.LessThanOrEqual(Int32.MinValue, value) && pc.LessThanOrEqual(value, Int32.MaxValue);
            }

#if DEBUG
            /// <summary>
            /// Return true if value is a string where each value is in the range of printable ASCII characters.
            /// </summary>
            private bool IsPrintableAscii(object value) {
                Debug.Assert(DynamicHelpers.GetPythonType(value).Equals(TypeCache.String));
                string strValue = (string)value;
                foreach (char c in strValue) {
                    if (!(LowestPrintableChar <= c && c <= HighestPrintableChar)) return false;
                }
                return true;
            }
#endif

            #endregion

            #region Output generation helpers

            private void Write(CodeContext/*!*/ context, string data) {
                _file.Write(context, data);
            }

            private void Write(CodeContext/*!*/ context, char data) {
                _file.Write(context, data);
            }

            private void WriteGet(CodeContext/*!*/ context, object obj) {
                Debug.Assert(MemoContains(obj));

                WriteGetOrPut(context, obj, true);
            }

            private void WriteGetOrPut(CodeContext context, object obj, bool isGet) {
                Debug.Assert(MemoContains(obj));
                
                if (_memo == null) {
                    WriteGetOrPut(context, isGet, _privMemo[obj]);
                } else {
                    WriteGetOrPut(context, isGet, (PythonTuple)_memo[PythonOps.Id(obj)]);
                }
            }

            private void WriteGetOrPut(CodeContext context, bool isGet, PythonTuple tup) {
                object index = tup[0];
                Debug.Assert(PythonContext.GetContext(context).GreaterThanOrEqual(index, 0));
                if (_protocol < 1) {
                    Write(context, isGet ? Opcode.Get : Opcode.Put);
                    WriteIntAsString(context, index);
                } else {
                    if (IsUInt8(context, index)) {
                        Write(context, isGet ? Opcode.BinGet : Opcode.BinPut);
                        WriteUInt8(context, index);
                    } else {
                        Write(context, isGet ? Opcode.LongBinGet : Opcode.LongBinPut);
                        WriteInt32(context, index);
                    }
                }
            }

            private void WriteGetOrPut(CodeContext context, bool isGet, int index) {
                if (_protocol < 1) {
                    Write(context, isGet ? Opcode.Get : Opcode.Put);
                    WriteIntAsString(context, index);
                } else if(index >= 0 && index <= 1 << 8) {
                    Write(context, isGet ? Opcode.BinGet : Opcode.BinPut);
                    WriteUInt8(context, index);
                } else {
                    Write(context, isGet ? Opcode.LongBinGet : Opcode.LongBinPut);
                    WriteInt32(context, index);
                }
            }

            private void WriteInitArgs(CodeContext/*!*/ context, object obj) {
                object getInitArgsCallable;
                if (PythonOps.TryGetBoundAttr(context, obj, "__getinitargs__", out getInitArgsCallable)) {
                    object initArgs = PythonCalls.Call(context, getInitArgsCallable);
                    if (!(initArgs is PythonTuple)) {
                        throw CannotPickle(context, obj, "__getinitargs__() must return tuple");
                    }
                    foreach (object arg in (PythonTuple)initArgs) {
                        Save(context, arg);
                    }
                }
            }

            private void WritePut(CodeContext/*!*/ context, object obj) {
                WriteGetOrPut(context, obj, false);
            }

            private void WritePut(CodeContext/*!*/ context, int index) {
                WriteGetOrPut(context, false, index);
            }

            private void WriteProto(CodeContext/*!*/ context) {
                Write(context, Opcode.Proto);
                WriteUInt8(context, _protocol);
            }

            /// <summary>
            /// Emit a series of opcodes that will set append all items indexed by iter
            /// to the object at the top of the stack. Use APPENDS if possible, but
            /// append no more than BatchSize items at a time.
            /// </summary>
            private void BatchAppends(CodeContext/*!*/ context, IEnumerator enumerator) {
                if (_protocol < 1) {
                    while (enumerator.MoveNext()) {
                        Save(context, enumerator.Current);
                        Write(context, Opcode.Append);
                    }
                } else {
                    object next;
                    if (enumerator.MoveNext()) {
                        next = enumerator.Current;
                    } else {
                        return;
                    }

                    int batchCompleted = 0;
                    object current;

                    // We do a one-item lookahead to avoid emitting an APPENDS for a
                    // single remaining item.
                    while (enumerator.MoveNext()) {
                        current = next;
                        next = enumerator.Current;

                        if (batchCompleted == _BATCHSIZE) {
                            Write(context, Opcode.Appends);
                            batchCompleted = 0;
                        }

                        if (batchCompleted == 0) {
                            Write(context, Opcode.Mark);
                        }

                        Save(context, current);
                        batchCompleted++;
                    }

                    if (batchCompleted == _BATCHSIZE) {
                        Write(context, Opcode.Appends);
                        batchCompleted = 0;
                    }
                    Save(context, next);
                    batchCompleted++;

                    if (batchCompleted > 1) {
                        Write(context, Opcode.Appends);
                    } else {
                        Write(context, Opcode.Append);
                    }
                }
            }

            /// <summary>
            /// Emit a series of opcodes that will set all (key, value) pairs indexed by
            /// iter in the object at the top of the stack. Use SETITEMS if possible,
            /// but append no more than BatchSize items at a time.
            /// </summary>
            private void BatchSetItems(CodeContext/*!*/ context, PythonDictionary dict) {
                KeyValuePair<object, object> kvTuple;
                using (var enumerator = dict._storage.GetEnumerator()) {
                    if (_protocol < 1) {
                        while (enumerator.MoveNext()) {
                            kvTuple = enumerator.Current;
                            Save(context, kvTuple.Key);
                            Save(context, kvTuple.Value);
                            Write(context, Opcode.SetItem);
                        }
                    } else {
                        object nextKey, nextValue;
                        if (enumerator.MoveNext()) {
                            kvTuple = enumerator.Current;
                            nextKey = kvTuple.Key;
                            nextValue = kvTuple.Value;
                        } else {
                            return;
                        }

                        int batchCompleted = 0;
                        object curKey, curValue;

                        // We do a one-item lookahead to avoid emitting a SETITEMS for a
                        // single remaining item.
                        while (enumerator.MoveNext()) {
                            curKey = nextKey;
                            curValue = nextValue;
                            kvTuple = enumerator.Current;
                            nextKey = kvTuple.Key;
                            nextValue = kvTuple.Value;

                            if (batchCompleted == _BATCHSIZE) {
                                Write(context, Opcode.SetItems);
                                batchCompleted = 0;
                            }

                            if (batchCompleted == 0) {
                                Write(context, Opcode.Mark);
                            }

                            Save(context, curKey);
                            Save(context, curValue);
                            batchCompleted++;
                        }

                        if (batchCompleted == _BATCHSIZE) {
                            Write(context, Opcode.SetItems);
                            batchCompleted = 0;
                        }
                        Save(context, nextKey);
                        Save(context, nextValue);
                        batchCompleted++;

                        if (batchCompleted > 1) {
                            Write(context, Opcode.SetItems);
                        } else {
                            Write(context, Opcode.SetItem);
                        }
                    }
                }
            }

            /// <summary>
            /// Emit a series of opcodes that will set all (key, value) pairs indexed by
            /// iter in the object at the top of the stack. Use SETITEMS if possible,
            /// but append no more than BatchSize items at a time.
            /// </summary>
            private void BatchSetItems(CodeContext/*!*/ context, IEnumerator enumerator) {
                PythonTuple kvTuple;
                if (_protocol < 1) {
                    while (enumerator.MoveNext()) {
                        kvTuple = (PythonTuple)enumerator.Current;
                        Save(context, kvTuple[0]);
                        Save(context, kvTuple[1]);
                        Write(context, Opcode.SetItem);
                    }
                } else {
                    object nextKey, nextValue;
                    if (enumerator.MoveNext()) {
                        kvTuple = (PythonTuple)enumerator.Current;
                        nextKey = kvTuple[0];
                        nextValue = kvTuple[1];
                    } else {
                        return;
                    }

                    int batchCompleted = 0;
                    object curKey, curValue;

                    // We do a one-item lookahead to avoid emitting a SETITEMS for a
                    // single remaining item.
                    while (enumerator.MoveNext()) {
                        curKey = nextKey;
                        curValue = nextValue;
                        kvTuple = (PythonTuple)enumerator.Current;
                        nextKey = kvTuple[0];
                        nextValue = kvTuple[1];

                        if (batchCompleted == _BATCHSIZE) {
                            Write(context, Opcode.SetItems);
                            batchCompleted = 0;
                        }

                        if (batchCompleted == 0) {
                            Write(context, Opcode.Mark);
                        }

                        Save(context, curKey);
                        Save(context, curValue);
                        batchCompleted++;
                    }

                    if (batchCompleted == _BATCHSIZE) {
                        Write(context, Opcode.SetItems);
                        batchCompleted = 0;
                    }
                    Save(context, nextKey);
                    Save(context, nextValue);
                    batchCompleted++;

                    if (batchCompleted > 1) {
                        Write(context, Opcode.SetItems);
                    } else {
                        Write(context, Opcode.SetItem);
                    }
                }
            }

            #endregion

            #region Other private helper methods

            private Exception CannotPickle(CodeContext/*!*/ context, object obj, string format, params object[] args) {
                StringBuilder msgBuilder = new StringBuilder();
                msgBuilder.Append("Can't pickle ");
                msgBuilder.Append(PythonOps.ToString(context, obj));
                if (format != null) {
                    msgBuilder.Append(": ");
                    msgBuilder.Append(String.Format(format, args));
                }
                return PythonExceptions.CreateThrowable(PickleError(context), msgBuilder.ToString());
            }

            
            /// <summary>
            /// Find the module for obj and ensure that obj is reachable in that module by the given name.
            /// 
            /// Throw PicklingError if any of the following are true:
            ///  - The module couldn't be determined.
            ///  - The module couldn't be loaded.
            ///  - The given name doesn't exist in the module.
            ///  - The given name is a different object than obj.
            /// 
            /// Otherwise, return the name of the module.
            /// 
            /// To determine which module obj lives in, obj.__module__ is used if available. The
            /// module named by obj.__module__ is loaded if needed. If obj has no __module__
            /// attribute, then each loaded module is searched. If a loaded module has an
            /// attribute with the given name, and that attribute is the same object as obj,
            /// then that module is used.
            /// </summary>
            private object FindModuleForGlobal(CodeContext/*!*/ context, object obj, object name) {
                object module;
                object moduleName;
                PythonType pt = obj as PythonType;
                if (pt != null) {
                    return PythonType.Get__module__(context, pt);
                } else if (PythonOps.TryGetBoundAttr(context, obj, "__module__", out moduleName)) {
                    // TODO: Global SystemState
                    LightExceptions.CheckAndThrow(Builtin.__import__(context, Converter.ConvertToString(moduleName)));

                    object foundObj;
                    if (Importer.TryGetExistingModule(context, Converter.ConvertToString(moduleName), out module) &&
                        PythonOps.TryGetBoundAttr(context, module, Converter.ConvertToString(name), out foundObj)) {
                        if (PythonOps.IsRetBool(foundObj, obj)) {
                            return moduleName;
                        } else {
                            throw CannotPickle(context, obj, "it's not the same object as {0}.{1}", moduleName, name);
                        }
                    } else {
                        throw CannotPickle(context, obj, "it's not found as {0}.{1}", moduleName, name);
                    }
                } else {
                    // No obj.__module__, so crawl through all loaded modules looking for obj
                    foreach (KeyValuePair<object, object> modulePair in context.LanguageContext.SystemStateModules) {
                        moduleName = modulePair.Key;
                        module = modulePair.Value;
                        object foundObj;
                        if (PythonOps.TryGetBoundAttr(context, module, Converter.ConvertToString(name), out foundObj) &&
                            PythonOps.IsRetBool(foundObj, obj)
                        ) {
                            return moduleName;
                        }
                    }
                    throw CannotPickle(context, obj, "could not determine its module");
                }

            }

            #endregion

        }

        #endregion

        #region Unpickler object

        public static UnpicklerObject Unpickler(CodeContext/*!*/ context, object file) {
            return new UnpicklerObject(context, file);
        }

        [Documentation("Unpickler(file) -> Unpickler object\n\n"
            + "An Unpickler object reads a pickle bytecode stream and creates corresponding\n"
            + "objects."
            + "\n"
            + "file: an object (such as an open file or a StringIO) with read(num_chars) and\n"
            + "    readline() methods that return strings"
            )]
        [PythonType("Unpickler"), PythonHidden]
        public class UnpicklerObject {
            private static readonly object _mark = new object();

            private FileInput _file;
            private List<object> _stack;
            private PythonDictionary _memo;
            private List<object> _privMemo;
            private object _pers_loader;

            public UnpicklerObject() {
                _privMemo = new List<object>(200);
            }

            public UnpicklerObject(CodeContext context, object file)
                : this() {
                _file = new PythonFileInput(context, file);
            }

            internal UnpicklerObject(CodeContext context, FileInput input)
                : this() {
                _file = input;
            }

            [Documentation("load() -> unpickled object\n\n"
                + "Read pickle data from the file object that was passed to the constructor and\n"
                + "return the corresponding unpickled objects."
               )]
            public object load(CodeContext/*!*/ context) {
                _stack = new List<object>(32);

                for (; ; ) {
                    var opcode = _file.ReadChar(context);

                    switch (opcode) {
                        case Opcode.Append: LoadAppend(context); break;
                        case Opcode.Appends: LoadAppends(context); break;
                        case Opcode.BinFloat: LoadBinFloat(context); break;
                        case Opcode.BinGet: LoadBinGet(context); break;
                        case Opcode.BinInt: LoadBinInt(context); break;
                        case Opcode.BinInt1: LoadBinInt1(context); break;
                        case Opcode.BinInt2: LoadBinInt2(context); break;
                        case Opcode.BinPersid: LoadBinPersid(context); break;
                        case Opcode.BinPut: LoadBinPut(context); break;
                        case Opcode.BinString: LoadBinString(context); break;
                        case Opcode.BinUnicode: LoadBinUnicode(context); break;
                        case Opcode.Build: LoadBuild(context); break;
                        case Opcode.Dict: LoadDict(context); break;
                        case Opcode.Dup: LoadDup(context); break;
                        case Opcode.EmptyDict: LoadEmptyDict(context); break;
                        case Opcode.EmptyList: LoadEmptyList(context); break;
                        case Opcode.EmptyTuple: LoadEmptyTuple(context); break;
                        case Opcode.Ext1: LoadExt1(context); break;
                        case Opcode.Ext2: LoadExt2(context); break;
                        case Opcode.Ext4: LoadExt4(context); break;
                        case Opcode.Float: LoadFloat(context); break;
                        case Opcode.Get: LoadGet(context); break;
                        case Opcode.Global: LoadGlobal(context); break;
                        case Opcode.Inst: LoadInst(context); break;
                        case Opcode.Int: LoadInt(context); break;
                        case Opcode.List: LoadList(context); break;
                        case Opcode.Long: LoadLong(context); break;
                        case Opcode.Long1: LoadLong1(context); break;
                        case Opcode.Long4: LoadLong4(context); break;
                        case Opcode.LongBinGet: LoadLongBinGet(context); break;
                        case Opcode.LongBinPut: LoadLongBinPut(context); break;
                        case Opcode.Mark: LoadMark(context); break;
                        case Opcode.NewFalse: LoadNewFalse(context); break;
                        case Opcode.NewObj: LoadNewObj(context); break;
                        case Opcode.NewTrue: LoadNewTrue(context); break;
                        case Opcode.NoneValue: LoadNoneValue(context); break;
                        case Opcode.Obj: LoadObj(context); break;
                        case Opcode.PersId: LoadPersId(context); break;
                        case Opcode.Pop: LoadPop(context); break;
                        case Opcode.PopMark: LoadPopMark(context); break;
                        case Opcode.Proto: LoadProto(context); break;
                        case Opcode.Put: LoadPut(context); break;
                        case Opcode.Reduce: LoadReduce(context); break;
                        case Opcode.SetItem: LoadSetItem(context); break;
                        case Opcode.SetItems: LoadSetItems(context); break;
                        case Opcode.ShortBinstring: LoadShortBinstring(context); break;
                        case Opcode.String: LoadString(context); break;
                        case Opcode.Tuple: LoadTuple(context); break;
                        case Opcode.Tuple1: LoadTuple1(context); break;
                        case Opcode.Tuple2: LoadTuple2(context); break;
                        case Opcode.Tuple3: LoadTuple3(context); break;
                        case Opcode.Unicode: LoadUnicode(context); break;
                        case Opcode.Stop: return PopStack();
                        default: throw CannotUnpickle(context, "invalid opcode: {0}", PythonOps.Repr(context, opcode));
                    }
                }
            }

            private object PopStack() {
                var res = _stack[_stack.Count - 1];
                _stack.RemoveAt(_stack.Count - 1);
                return res;
            }

            private object PeekStack() {
                return _stack[_stack.Count - 1];
            }

            public object[] StackGetSliceAsArray(int start) {
                object[] res = new object[_stack.Count - start];
                for (int i = 0; i < res.Length; i++) {
                    res[i] = _stack[i + start];
                }
                return res;
            }

            [Documentation("noload() -> unpickled object\n\n"
                // 1234567890123456789012345678901234567890123456789012345678901234567890123456789
                + "Like load(), but don't import any modules or create create any instances of\n"
                + "user-defined types. (Builtin objects such as ints, tuples, etc. are created as\n"
                + "with load().)\n"
                + "\n"
                + "This is primarily useful for scanning a pickle for persistent ids without\n"
                + "incurring the overhead of completely unpickling an object. See the pickle\n"
                + "module documentation for more information about persistent ids."
               )]
            public void noload(CodeContext/*!*/ context) {
                throw PythonOps.NotImplementedError("noload() is not implemented");
            }

            private Exception CannotUnpickle(CodeContext/*!*/ context, string format, params object[] args) {
                return PythonExceptions.CreateThrowable(UnpicklingError(context), String.Format(format, args));
            }

            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
            public PythonDictionary memo {
                get {
                    if (_memo == null) {
                        var newMemo = new PythonDictionary();
                        for (int i = 0; i < _privMemo.Count; i++) {
                            if (_privMemo[i] != _mark) {
                                newMemo[i] = _privMemo[i];
                            }
                        }
                        _memo = newMemo;
                    }
                    return _memo; 
                }
                set { 
                    _memo = value;
                    _privMemo = null;
                }
            }

            public object persistent_load {
                get {
                    return _pers_loader;
                }
                set {
                    _pers_loader = value;
                }
            }

            private object MemoGet(CodeContext/*!*/ context, int key) {
                object value;
                if (_memo != null) {
                    if (_memo.TryGetValue(key, out value)) {
                        return value;
                    }
                } else if (key < _privMemo.Count && (value = _privMemo[key]) != _mark) {
                    return value;
                }
                
                throw PythonExceptions.CreateThrowable(BadPickleGet(context), String.Format("memo key {0} not found", key));
            }

            private void MemoPut(int key, object value) {
                if (_memo != null) {
                    _memo[key] = value;
                } else {
                    while (key >= _privMemo.Count) {
                        _privMemo.Add(_mark);
                    }
                    _privMemo[key] = value;
                }
            }

            private int GetMarkIndex(CodeContext/*!*/ context) {
                int i = _stack.Count - 1;
                while (i > 0 && _stack[i] != _mark) i -= 1;
                if (i == -1) throw CannotUnpickle(context, "mark not found");
                return i;
            }

            private string Read(CodeContext/*!*/ context, int size) {
                string res = _file.Read(context, size);
                if (res.Length < size) {
                    throw PythonOps.EofError("unexpected EOF while unpickling");
                }
                return res;
            }

            private string ReadLineNoNewline(CodeContext/*!*/ context) {
                string raw = _file.ReadLine(context);
                return raw.Substring(0, raw.Length - 1);
            }

            private object ReadFloatString(CodeContext/*!*/ context) {
                return DoubleOps.__new__(context, TypeCache.Double, ReadLineNoNewline(context));
            }

            private double ReadFloat64(CodeContext/*!*/ context) {
                int index = 0;
                return PythonStruct.CreateDoubleValue(context, ref index, false, Read(context, 8));
            }

            private object ReadIntFromString(CodeContext/*!*/ context) {
                string raw = ReadLineNoNewline(context);
                if ("00" == raw) return ScriptingRuntimeHelpers.False;
                else if ("01" == raw) return ScriptingRuntimeHelpers.True;
                return Int32Ops.__new__(context, TypeCache.Int32, raw);
            }

            private int ReadInt32(CodeContext/*!*/ context) {
                return _file.ReadInt(context);
            }

            private object ReadLongFromString(CodeContext/*!*/ context) {
                return BigIntegerOps.__new__(context, TypeCache.BigInteger, ReadLineNoNewline(context));
            }

            private object ReadLong(CodeContext/*!*/ context, int size) {
                return new BigInteger(Read(context, size).MakeByteArray());
            }

            private char ReadUInt8(CodeContext/*!*/ context) {
                return _file.ReadChar(context);
            }

            private ushort ReadUInt16(CodeContext/*!*/ context) {
                int index = 0;
                return PythonStruct.CreateUShortValue(context, ref index, true, Read(context, 2));
            }

            public object find_global(CodeContext/*!*/ context, object module, object attr) {
                object moduleObject;
                if (!Importer.TryGetExistingModule(context, Converter.ConvertToString(module), out moduleObject)) {
                    LightExceptions.CheckAndThrow(Builtin.__import__(context, Converter.ConvertToString(module)));
                    moduleObject = context.LanguageContext.SystemStateModules[module];
                }
                return PythonOps.GetBoundAttr(context, moduleObject, Converter.ConvertToString(attr));
            }

            private object MakeInstance(CodeContext/*!*/ context, object cls, object[] args) {
                OldClass oc = cls as OldClass;
                if (oc != null) {
                    OldInstance inst = new OldInstance(context, oc);
                    if (args.Length != 0 || PythonOps.HasAttr(context, cls, "__getinitargs__")) {
                        PythonOps.CallWithContext(context, PythonOps.GetBoundAttr(context, inst, "__init__"), args);
                    }
                    return inst;
                }
                return PythonOps.CallWithContext(context, cls, args);
            }

            private void PopMark(int markIndex) {
                for (int i = _stack.Count - 1; i >= markIndex; i--) {
                    _stack.RemoveAt(i);
                }
            }

            /// <summary>
            /// Interpret everything from markIndex to the top of the stack as a sequence
            /// of key, value, key, value, etc. Set dict[key] = value for each. Pop
            /// everything from markIndex up when done.
            /// </summary>
            private void SetItems(PythonDictionary dict, int markIndex) {
                var storage = dict._storage;
                storage.EnsureCapacityNoLock((_stack.Count - (markIndex + 1)) / 2);
                for (int i = markIndex + 1; i < _stack.Count; i += 2) {
                    storage.AddNoLock(ref dict._storage, _stack[i], _stack[i + 1]);
                }
                PopMark(markIndex);
            }

            private void LoadAppend(CodeContext/*!*/ context) {
                object item = PopStack();
                object seq = PeekStack();
                if (seq is List) {
                    ((List)seq).append(item);
                } else {
                    PythonCalls.Call(context, PythonOps.GetBoundAttr(context, seq, "append"), item);
                }
            }

            private void LoadAppends(CodeContext/*!*/ context) {
                int markIndex = GetMarkIndex(context);
                List seq = (List)_stack[markIndex - 1];
                for (int i = markIndex + 1; i < _stack.Count; i++) {
                    seq.AddNoLock(_stack[i]);
                }
                PopMark(markIndex);
            }

            private void LoadBinFloat(CodeContext/*!*/ context) {
                _stack.Add(ReadFloat64(context));
            }

            private void LoadBinGet(CodeContext/*!*/ context) {
                _stack.Add(MemoGet(context, ReadUInt8(context)));
            }

            private void LoadBinInt(CodeContext/*!*/ context) {
                _stack.Add(ReadInt32(context));
            }

            private void LoadBinInt1(CodeContext/*!*/ context) {
                _stack.Add((int)ReadUInt8(context));
            }

            private void LoadBinInt2(CodeContext/*!*/ context) {
                _stack.Add((int)ReadUInt16(context));
            }

            private void LoadBinPersid(CodeContext/*!*/ context) {
                if (_pers_loader == null) throw CannotUnpickle(context, "cannot unpickle binary persistent ID w/o persistent_load");

                _stack.Add(PythonContext.GetContext(context).CallSplat(_pers_loader, PopStack()));
            }

            private void LoadBinPut(CodeContext/*!*/ context) {
                MemoPut(ReadUInt8(context), PeekStack());
            }

            private void LoadBinString(CodeContext/*!*/ context) {
                _stack.Add(Read(context, ReadInt32(context)));
            }

            private void LoadBinUnicode(CodeContext/*!*/ context) {
                string text = Read(context, ReadInt32(context));
                for (int i = 0; i < text.Length; i++) {
                    if (text[i] >= 128) {
                        _stack.Add(StringOps.decode(context, text, "utf-8", "strict"));
                        return;
                    }
                }
                _stack.Add(text);
            }

            private void LoadBuild(CodeContext/*!*/ context) {
                object arg = PopStack();
                object inst = PeekStack();
                object setStateCallable;
                if (PythonOps.TryGetBoundAttr(context, inst, "__setstate__", out setStateCallable)) {
                    PythonOps.CallWithContext(context, setStateCallable, arg);
                    return;
                }

                PythonDictionary dict;
                PythonDictionary slots;
                if (arg == null) {
                    dict = null;
                    slots = null;
                } else if (arg is PythonDictionary) {
                    dict = (PythonDictionary)arg;
                    slots = null;
                } else if (arg is PythonTuple) {
                    PythonTuple argsTuple = (PythonTuple)arg;
                    if (argsTuple.__len__() != 2) {
                        throw PythonOps.ValueError("state for object without __setstate__ must be None, dict, or 2-tuple");
                    }
                    dict = (PythonDictionary)argsTuple[0];
                    slots = (PythonDictionary)argsTuple[1];
                } else {
                    throw PythonOps.ValueError("state for object without __setstate__ must be None, dict, or 2-tuple");
                }

                if (dict != null) {
                    object instDict;
                    if (PythonOps.TryGetBoundAttr(context, inst, "__dict__", out instDict)) {
                        PythonDictionary realDict = instDict as PythonDictionary;
                        if (realDict != null) {
                            realDict.update(context, dict);
                        } else {
                            object updateCallable;
                            if (PythonOps.TryGetBoundAttr(context, instDict, "update", out updateCallable)) {
                                PythonOps.CallWithContext(context, updateCallable, dict);
                            } else {
                                throw CannotUnpickle(context, "could not update __dict__ {0} when building {1}", dict, inst);
                            }
                        }
                    }
                }

                if (slots != null) {
                    foreach (object key in (IEnumerable)slots) {
                        PythonOps.SetAttr(context, inst, (string)key, slots[key]);
                    }
                }
            }

            private void LoadDict(CodeContext/*!*/ context) {
                int markIndex = GetMarkIndex(context);
                PythonDictionary dict = new PythonDictionary((_stack.Count - 1 - markIndex) / 2);
                SetItems(dict, markIndex);
                _stack.Add(dict);
            }

            private void LoadDup(CodeContext/*!*/ context) {
                _stack.Add(PeekStack());
            }

            private void LoadEmptyDict(CodeContext/*!*/ context) {
                _stack.Add(new PythonDictionary(new CommonDictionaryStorage()));
            }

            private void LoadEmptyList(CodeContext/*!*/ context) {
                _stack.Add(PythonOps.MakeList());
            }

            private void LoadEmptyTuple(CodeContext/*!*/ context) {
                _stack.Add(PythonTuple.MakeTuple());
            }

            private void LoadExt1(CodeContext/*!*/ context) {
                PythonTuple global = (PythonTuple)PythonCopyReg.GetInvertedRegistry(context)[(int)ReadUInt8(context)];
                _stack.Add(find_global(context, global[0], global[1]));
            }

            private void LoadExt2(CodeContext/*!*/ context) {
                PythonTuple global = (PythonTuple)PythonCopyReg.GetInvertedRegistry(context)[(int)ReadUInt16(context)];
                _stack.Add(find_global(context, global[0], global[1]));
            }

            private void LoadExt4(CodeContext/*!*/ context) {
                PythonTuple global = (PythonTuple)PythonCopyReg.GetInvertedRegistry(context)[ReadInt32(context)];
                _stack.Add(find_global(context, global[0], global[1]));
            }

            private void LoadFloat(CodeContext/*!*/ context) {
                _stack.Add(ReadFloatString(context));
            }

            private void LoadGet(CodeContext/*!*/ context) {
                try {
                    _stack.Add(MemoGet(context, (int)ReadIntFromString(context)));
                } catch (ArgumentException) {
                    throw PythonExceptions.CreateThrowable(BadPickleGet(context), "while executing GET: invalid integer value");
                }
            }

            private void LoadGlobal(CodeContext/*!*/ context) {
                string module = ReadLineNoNewline(context);
                string attr = ReadLineNoNewline(context);
                _stack.Add(find_global(context, module, attr));
            }

            private void LoadInst(CodeContext/*!*/ context) {
                LoadGlobal(context);
                object cls = PopStack();
                if (cls is OldClass || cls is PythonType) {
                    int markIndex = GetMarkIndex(context);
                    object[] args = StackGetSliceAsArray(markIndex + 1);
                    PopMark(markIndex);

                    _stack.Add(MakeInstance(context, cls, args));
                } else {
                    throw PythonOps.TypeError("expected class or type after INST, got {0}", DynamicHelpers.GetPythonType(cls));
                }
            }

            private void LoadInt(CodeContext/*!*/ context) {
                _stack.Add(ReadIntFromString(context));
            }

            private void LoadList(CodeContext/*!*/ context) {
                int markIndex = GetMarkIndex(context);
                List list = List.FromArrayNoCopy(StackGetSliceAsArray(markIndex + 1));
                PopMark(markIndex);
                _stack.Add(list);
            }

            private void LoadLong(CodeContext/*!*/ context) {
                _stack.Add(ReadLongFromString(context));
            }

            private void LoadLong1(CodeContext/*!*/ context) {
                int size = ReadUInt8(context);
                if (size == 4) {
                    _stack.Add((BigInteger)ReadInt32(context));
                } else {
                    _stack.Add(ReadLong(context, size));
                }
            }

            private void LoadLong4(CodeContext/*!*/ context) {
                _stack.Add(ReadLong(context, ReadInt32(context)));
            }

            private void LoadLongBinGet(CodeContext/*!*/ context) {
                _stack.Add(MemoGet(context, (int)ReadInt32(context)));
            }

            private void LoadLongBinPut(CodeContext/*!*/ context) {
                MemoPut(ReadInt32(context), PeekStack());
            }

            private void LoadMark(CodeContext/*!*/ context) {
                _stack.Add(_mark);
            }

            private void LoadNewFalse(CodeContext/*!*/ context) {
                _stack.Add(ScriptingRuntimeHelpers.False);
            }

            private void LoadNewObj(CodeContext/*!*/ context) {
                PythonTuple args = PopStack() as PythonTuple;
                if (args == null) {
                    throw PythonOps.TypeError("expected tuple as second argument to NEWOBJ, got {0}", DynamicHelpers.GetPythonType(args));
                }

                PythonType cls = PopStack() as PythonType;
                if (cls == null) {
                    throw PythonOps.TypeError("expected new-style type as first argument to NEWOBJ, got {0}", DynamicHelpers.GetPythonType(args));
                }

                PythonTypeSlot dts;
                object value;
                if (cls.TryResolveSlot(context, "__new__", out dts) &&
                    dts.TryGetValue(context, null, cls, out value)) {
                    object[] newargs = new object[args.__len__() + 1];
                    ((ICollection)args).CopyTo(newargs, 1);
                    newargs[0] = cls;

                    _stack.Add(PythonOps.CallWithContext(context, value, newargs));
                    return;
                }
                
                throw PythonOps.TypeError("didn't find __new__");
            }

            private void LoadNewTrue(CodeContext/*!*/ context) {
                _stack.Add(ScriptingRuntimeHelpers.True);
            }

            private void LoadNoneValue(CodeContext/*!*/ context) {
                _stack.Add(null);
            }

            private void LoadObj(CodeContext/*!*/ context) {
                int markIndex = GetMarkIndex(context);
                if ((markIndex + 1) >= _stack.Count) {
                    throw PythonExceptions.CreateThrowable(UnpicklingError(context), "could not find MARK");
                }

                object cls = _stack[markIndex + 1];
                if (cls is OldClass || cls is PythonType) {
                    object[] args = StackGetSliceAsArray(markIndex + 2);
                    PopMark(markIndex);
                    _stack.Add(MakeInstance(context, cls, args));
                } else {
                    throw PythonOps.TypeError("expected class or type as first argument to INST, got {0}", DynamicHelpers.GetPythonType(cls));
                }
            }

            private void LoadPersId(CodeContext/*!*/ context) {
                if (_pers_loader == null) {
                    throw CannotUnpickle(context, "A load persistent ID instruction is present but no persistent_load function is available");
                }
                _stack.Add(PythonContext.GetContext(context).CallSplat(_pers_loader, ReadLineNoNewline(context)));
            }

            private void LoadPop(CodeContext/*!*/ context) {
                PopStack();
            }

            private void LoadPopMark(CodeContext/*!*/ context) {
                PopMark(GetMarkIndex(context));
            }

            private void LoadProto(CodeContext/*!*/ context) {
                int proto = ReadUInt8(context);
                if (proto > 2) throw PythonOps.ValueError("unsupported pickle protocol: {0}", proto);
                // discard result
            }

            private void LoadPut(CodeContext/*!*/ context) {
                MemoPut((int)ReadIntFromString(context), PeekStack());
            }

            private void LoadReduce(CodeContext/*!*/ context) {
                object args = PopStack();
                object callable = PopStack();
                if (args == null) {
                    _stack.Add(PythonCalls.Call(context, PythonOps.GetBoundAttr(context, callable, "__basicnew__")));
                } else if (args.GetType() != typeof(PythonTuple)) {
                    throw PythonOps.TypeError(
                        "while executing REDUCE, expected tuple at the top of the stack, but got {0}",
                        DynamicHelpers.GetPythonType(args)
                    );
                }

                _stack.Add(PythonCalls.Call(context, callable, ((PythonTuple)args)._data));
            }

            private void LoadSetItem(CodeContext/*!*/ context) {
                object value = PopStack();
                object key = PopStack();
                PythonDictionary dict = PeekStack() as PythonDictionary;
                if (dict == null) {
                    throw PythonOps.TypeError(
                        "while executing SETITEM, expected dict at stack[-3], but got {0}",
                        DynamicHelpers.GetPythonType(PeekStack())
                    );
                }
                dict[key] = value;
            }

            private void LoadSetItems(CodeContext/*!*/ context) {
                int markIndex = GetMarkIndex(context);
                PythonDictionary dict = _stack[markIndex - 1] as PythonDictionary;
                
                if (dict == null) {
                    throw PythonOps.TypeError(
                        "while executing SETITEMS, expected dict below last mark, but got {0}",
                        DynamicHelpers.GetPythonType(_stack[markIndex - 1])
                    );
                }
                SetItems(dict, markIndex);
            }

            private void LoadShortBinstring(CodeContext/*!*/ context) {
                _stack.Add(Read(context, ReadUInt8(context)));
            }

            private void LoadString(CodeContext/*!*/ context) {
                string repr = ReadLineNoNewline(context);
                if (repr.Length < 2 ||
                    !(
                    repr[0] == '"' && repr[repr.Length - 1] == '"' ||
                    repr[0] == '\'' && repr[repr.Length - 1] == '\''
                    )
                ) {
                    throw PythonOps.ValueError("while executing STRING, expected string that starts and ends with quotes");
                }
                _stack.Add(StringOps.decode(context, repr.Substring(1, repr.Length - 2), "string-escape", "strict"));
            }

            private void LoadTuple(CodeContext/*!*/ context) {
                int markIndex = GetMarkIndex(context);
                PythonTuple tuple = PythonTuple.MakeTuple(StackGetSliceAsArray(markIndex + 1));
                PopMark(markIndex);
                _stack.Add(tuple);
            }

            private void LoadTuple1(CodeContext/*!*/ context) {
                object item0 = PopStack();
                _stack.Add(PythonTuple.MakeTuple(item0));
            }

            private void LoadTuple2(CodeContext/*!*/ context) {
                object item1 = PopStack();
                object item0 = PopStack();
                _stack.Add(PythonTuple.MakeTuple(item0, item1));
            }

            private void LoadTuple3(CodeContext/*!*/ context) {
                object item2 = PopStack();
                object item1 = PopStack();
                object item0 = PopStack();
                _stack.Add(PythonTuple.MakeTuple(item0, item1, item2));
            }

            private void LoadUnicode(CodeContext/*!*/ context) {
                _stack.Add(StringOps.decode(context, ReadLineNoNewline(context), "raw-unicode-escape", "strict"));
            }
        }

        #endregion

        private static PythonType PicklingError(CodeContext/*!*/ context) {
            return (PythonType)PythonContext.GetContext(context).GetModuleState("PicklingError");
        }

        private static PythonType PickleError(CodeContext/*!*/ context) {
            return (PythonType)PythonContext.GetContext(context).GetModuleState("PickleError");
        }

        private static PythonType UnpicklingError(CodeContext/*!*/ context) {
            return (PythonType)PythonContext.GetContext(context).GetModuleState("UnpicklingError");
        }

        private static PythonType BadPickleGet(CodeContext/*!*/ context) {
            return (PythonType)PythonContext.GetContext(context).GetModuleState("BadPickleGet");
        }

        class ReferenceEqualityComparer : IEqualityComparer<object> {
            public static ReferenceEqualityComparer Instance = new ReferenceEqualityComparer();
            #region IEqualityComparer<object> Members

            public new bool Equals(object x, object y) {
                return x == y;
            }

            public int GetHashCode(object obj) {
                return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(obj);
            }

            #endregion
        }

    }
}
Back to Top