forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_pickle.cs
More file actions
2317 lines (1992 loc) · 100 KB
/
_pickle.cs
File metadata and controls
2317 lines (1992 loc) · 100 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
#if FALSE // TODO: update native pickling
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Text;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;
using IronPython.Runtime;
using IronPython.Runtime.Exceptions;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;
[assembly: PythonModule("_pickle", 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", "_pickle");
context.EnsureModuleException("PicklingError", dict, "PicklingError", "_pickle");
context.EnsureModuleException("UnpicklingError", dict, "UnpicklingError", "_pickle");
context.EnsureModuleException("UnpickleableError", dict, "UnpickleableError", "_pickle");
context.EnsureModuleException("BadPickleGet", dict, "BadPickleGet", "_pickle");
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, object protocol=null, object bin=null) {
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, object protocol=null, object bin=null) {
//??? 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]IList<byte> @string) {
return new UnpicklerObject(context, new PythonStringInput(PythonOps.MakeString(@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, object file=null, object protocol=null, object bin=null) {
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(PythonList)] = SaveList;
_dispatchTable[typeof(PythonFunction)] = SaveGlobal;
_dispatchTable[typeof(BuiltinFunction)] = SaveGlobal;
_dispatchTable[typeof(PythonType)] = SaveGlobal;
}
#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 PythonIOModule.StringIO(context, "", "\n"));
} 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 PythonIOModule.StringIO(context, "", "\n"));
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(context.LanguageContext.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.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 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.PythonList), "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());
}
private static readonly BigInteger MaxInt = new BigInteger(Int32.MaxValue);
private static readonly BigInteger MinInt = new BigInteger(Int32.MinValue);
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
/// copyreg.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);
}
}