PageRenderTime 63ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 1ms

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

http://github.com/IronLanguages/main
C# | 2389 lines | 2156 code | 155 blank | 78 comment | 223 complexity | a3c6fcc07ac2da828908469cc2621697 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.Diagnostics;
  19. using System.IO;
  20. using System.Runtime.InteropServices;
  21. using System.Text;
  22. using IronPython.Runtime;
  23. using IronPython.Runtime.Exceptions;
  24. using IronPython.Runtime.Operations;
  25. using IronPython.Runtime.Types;
  26. using Microsoft.Scripting;
  27. using Microsoft.Scripting.Runtime;
  28. using Microsoft.Scripting.Utils;
  29. #if FEATURE_NUMERICS
  30. using System.Numerics;
  31. #else
  32. using Microsoft.Scripting.Math;
  33. #endif
  34. [assembly: PythonModule("cPickle", typeof(IronPython.Modules.PythonPickle))]
  35. namespace IronPython.Modules {
  36. public static class PythonPickle {
  37. public const string __doc__ = "Fast object serialization/deserialization.\n\n"
  38. + "Differences from CPython:\n"
  39. + " - does not implement the undocumented fast mode\n";
  40. [System.Runtime.CompilerServices.SpecialName]
  41. public static void PerformModuleReload(PythonContext/*!*/ context, PythonDictionary/*!*/ dict) {
  42. context.EnsureModuleException("PickleError", dict, "PickleError", "cPickle");
  43. context.EnsureModuleException("PicklingError", dict, "PicklingError", "cPickle");
  44. context.EnsureModuleException("UnpicklingError", dict, "UnpicklingError", "cPickle");
  45. context.EnsureModuleException("UnpickleableError", dict, "UnpickleableError", "cPickle");
  46. context.EnsureModuleException("BadPickleGet", dict, "BadPickleGet", "cPickle");
  47. dict["__builtins__"] = context.BuiltinModuleInstance;
  48. dict["compatible_formats"] = PythonOps.MakeList("1.0", "1.1", "1.2", "1.3", "2.0");
  49. }
  50. private static readonly PythonStruct.Struct _float64 = PythonStruct.Struct.Create(">d");
  51. private const int highestProtocol = 2;
  52. public const string __version__ = "1.71";
  53. public const string format_version = "2.0";
  54. public static int HIGHEST_PROTOCOL {
  55. get { return highestProtocol; }
  56. }
  57. private const string Newline = "\n";
  58. #region Public module-level functions
  59. [Documentation("dump(obj, file, protocol=0) -> None\n\n"
  60. + "Pickle obj and write the result to file.\n"
  61. + "\n"
  62. + "See documentation for Pickler() for a description the file, protocol, and\n"
  63. + "(deprecated) bin parameters."
  64. )]
  65. public static void dump(CodeContext/*!*/ context, object obj, object file, [DefaultParameterValue(null)] object protocol, [DefaultParameterValue(null)] object bin) {
  66. PicklerObject/*!*/ pickler = new PicklerObject(context, file, protocol, bin);
  67. pickler.dump(context, obj);
  68. }
  69. [Documentation("dumps(obj, protocol=0) -> pickle string\n\n"
  70. + "Pickle obj and return the result as a string.\n"
  71. + "\n"
  72. + "See the documentation for Pickler() for a description of the protocol and\n"
  73. + "(deprecated) bin parameters."
  74. )]
  75. public static string dumps(CodeContext/*!*/ context, object obj, [DefaultParameterValue(null)] object protocol, [DefaultParameterValue(null)] object bin) {
  76. //??? possible perf enhancement: use a C# TextWriter-backed IFileOutput and
  77. // thus avoid Python call overhead. Also do similar thing for LoadFromString.
  78. var stringIO = new StringBuilderOutput();
  79. PicklerObject/*!*/ pickler = new PicklerObject(context, stringIO, protocol, bin);
  80. pickler.dump(context, obj);
  81. return stringIO.GetString();
  82. }
  83. [Documentation("load(file) -> unpickled object\n\n"
  84. + "Read pickle data from the open file object and return the corresponding\n"
  85. + "unpickled object. Data after the first pickle found is ignored, but the file\n"
  86. + "cursor is not reset, so if a file objects contains multiple pickles, then\n"
  87. + "load() may be called multiple times to unpickle them.\n"
  88. + "\n"
  89. + "file: an object (such as an open file or a StringIO) with read(num_chars) and\n"
  90. + " readline() methods that return strings\n"
  91. + "\n"
  92. + "load() automatically determines if the pickle data was written in binary or\n"
  93. + "text mode."
  94. )]
  95. public static object load(CodeContext/*!*/ context, object file) {
  96. return new UnpicklerObject(context, file).load(context);
  97. }
  98. [Documentation("loads(string) -> unpickled object\n\n"
  99. + "Read a pickle object from a string, unpickle it, and return the resulting\n"
  100. + "reconstructed object. Characters in the string beyond the end of the first\n"
  101. + "pickle are ignored."
  102. )]
  103. public static object loads(CodeContext/*!*/ context, [BytesConversion]string @string) {
  104. return new UnpicklerObject(context, new PythonStringInput(@string)).load(context);
  105. }
  106. #endregion
  107. #region File I/O wrappers
  108. /// <summary>
  109. /// Interface for "file-like objects" that implement the protocol needed by load() and friends.
  110. /// This enables the creation of thin wrappers that make fast .NET types and slow Python types look the same.
  111. /// </summary>
  112. internal abstract class FileInput {
  113. public abstract string Read(CodeContext/*!*/ context, int size);
  114. public abstract string ReadLine(CodeContext/*!*/ context);
  115. public virtual string ReadLineNoNewLine(CodeContext/*!*/ context) {
  116. var raw = ReadLine(context);
  117. return raw.Substring(0, raw.Length - 1);
  118. }
  119. public virtual char ReadChar(CodeContext context) {
  120. string res = Read(context, 1);
  121. if (res.Length < 1) {
  122. throw PythonOps.EofError("unexpected EOF while unpickling");
  123. }
  124. return res[0];
  125. }
  126. public virtual int ReadInt(CodeContext context) {
  127. return (int)ReadChar(context) |
  128. ((int)ReadChar(context)) << 8 |
  129. ((int)ReadChar(context)) << 16 |
  130. ((int)ReadChar(context)) << 24;
  131. }
  132. }
  133. /// <summary>
  134. /// Interface for "file-like objects" that implement the protocol needed by dump() and friends.
  135. /// This enables the creation of thin wrappers that make fast .NET types and slow Python types look the same.
  136. /// </summary>
  137. internal abstract class FileOutput {
  138. private readonly char[] int32chars = new char[4];
  139. public abstract void Write(CodeContext/*!*/ context, string data);
  140. public virtual void Write(CodeContext context, int data) {
  141. int32chars[0] = (char)(int)((data & 0xff));
  142. int32chars[1] = (char)(int)((data >> 8) & 0xff);
  143. int32chars[2] = (char)(int)((data >> 16) & 0xff);
  144. int32chars[3] = (char)(int)((data >> 24) & 0xff);
  145. Write(context, new string(int32chars));
  146. }
  147. public virtual void Write(CodeContext context, char data) {
  148. Write(context, ScriptingRuntimeHelpers.CharToString(data));
  149. }
  150. }
  151. private class PythonFileInput : FileInput {
  152. private object _readMethod;
  153. private object _readLineMethod;
  154. public PythonFileInput(CodeContext/*!*/ context, object file) {
  155. if (!PythonOps.TryGetBoundAttr(context, file, "read", out _readMethod) ||
  156. !PythonOps.IsCallable(context, _readMethod) ||
  157. !PythonOps.TryGetBoundAttr(context, file, "readline", out _readLineMethod) ||
  158. !PythonOps.IsCallable(context, _readLineMethod)
  159. ) {
  160. throw PythonOps.TypeError("argument must have callable 'read' and 'readline' attributes");
  161. }
  162. }
  163. public override string Read(CodeContext/*!*/ context, int size) {
  164. return Converter.ConvertToString(PythonCalls.Call(context, _readMethod, size));
  165. }
  166. public override string ReadLine(CodeContext/*!*/ context) {
  167. return Converter.ConvertToString(PythonCalls.Call(context, _readLineMethod));
  168. }
  169. }
  170. internal class PythonStringInput : FileInput {
  171. private readonly string _data;
  172. int _offset;
  173. public PythonStringInput(string data) {
  174. _data = data;
  175. }
  176. public override string Read(CodeContext context, int size) {
  177. var res = _data.Substring(_offset, size);
  178. _offset += size;
  179. return res;
  180. }
  181. public override string ReadLine(CodeContext context) {
  182. return ReadLineWorker(true);
  183. }
  184. public override string ReadLineNoNewLine(CodeContext context) {
  185. return ReadLineWorker(false);
  186. }
  187. public override char ReadChar(CodeContext context) {
  188. if (_offset < _data.Length) {
  189. return _data[_offset++];
  190. }
  191. throw PythonOps.EofError("unexpected EOF while unpickling");
  192. }
  193. public override int ReadInt(CodeContext context) {
  194. if (_offset + 4 <= _data.Length) {
  195. int res = _data[_offset]|
  196. ((int)_data[_offset + 1]) << 8 |
  197. ((int)_data[_offset + 2]) << 16 |
  198. ((int)_data[_offset + 3]) << 24;
  199. _offset += 4;
  200. return res;
  201. }
  202. throw PythonOps.EofError("unexpected EOF while unpickling");
  203. }
  204. private string ReadLineWorker(bool includeNewLine) {
  205. string res;
  206. for (int i = _offset; i < _data.Length; i++) {
  207. if (_data[i] == '\n') {
  208. res = _data.Substring(_offset, i - _offset + (includeNewLine ? 1 : 0));
  209. _offset = i + 1;
  210. return res;
  211. }
  212. }
  213. res = _data.Substring(_offset);
  214. _offset = _data.Length;
  215. return res;
  216. }
  217. }
  218. private class PythonFileLikeOutput : FileOutput {
  219. private object _writeMethod;
  220. public PythonFileLikeOutput(CodeContext/*!*/ context, object file) {
  221. if (!PythonOps.TryGetBoundAttr(context, file, "write", out _writeMethod) ||
  222. !PythonOps.IsCallable(context, this._writeMethod)
  223. ) {
  224. throw PythonOps.TypeError("argument must have callable 'write' attribute");
  225. }
  226. }
  227. public override void Write(CodeContext/*!*/ context, string data) {
  228. PythonCalls.Call(context, _writeMethod, data);
  229. }
  230. }
  231. private class PythonFileOutput : FileOutput {
  232. private readonly PythonFile _file;
  233. public PythonFileOutput(PythonFile file) {
  234. _file = file;
  235. }
  236. public override void Write(CodeContext/*!*/ context, string data) {
  237. _file.write(data);
  238. }
  239. }
  240. private class StringBuilderOutput : FileOutput {
  241. private readonly StringBuilder _builder = new StringBuilder(4096);
  242. public string GetString() {
  243. return _builder.ToString();
  244. }
  245. public override void Write(CodeContext context, char data) {
  246. _builder.Append(data);
  247. }
  248. public override void Write(CodeContext context, int data) {
  249. _builder.Append((char)(int)((data) & 0xff));
  250. _builder.Append((char)(int)((data >> 8) & 0xff));
  251. _builder.Append((char)(int)((data >> 16) & 0xff));
  252. _builder.Append((char)(int)((data >> 24) & 0xff));
  253. }
  254. public override void Write(CodeContext context, string data) {
  255. _builder.Append(data);
  256. }
  257. }
  258. private class PythonReadableFileOutput : PythonFileLikeOutput {
  259. private object _getValueMethod;
  260. public PythonReadableFileOutput(CodeContext/*!*/ context, object file)
  261. : base(context, file) {
  262. if (!PythonOps.TryGetBoundAttr(context, file, "getvalue", out _getValueMethod) ||
  263. !PythonOps.IsCallable(context, _getValueMethod)
  264. ) {
  265. throw PythonOps.TypeError("argument must have callable 'getvalue' attribute");
  266. }
  267. }
  268. public object GetValue(CodeContext/*!*/ context) {
  269. return PythonCalls.Call(context, _getValueMethod);
  270. }
  271. }
  272. #endregion
  273. #region Opcode constants
  274. internal static class Opcode {
  275. public const char Append = 'a';
  276. public const char Appends = 'e';
  277. public const char BinFloat = 'G';
  278. public const char BinGet = 'h';
  279. public const char BinInt = 'J';
  280. public const char BinInt1 = 'K';
  281. public const char BinInt2 = 'M';
  282. public const char BinPersid = 'Q';
  283. public const char BinPut = 'q';
  284. public const char BinString = 'T';
  285. public const char BinUnicode = 'X';
  286. public const char Build = 'b';
  287. public const char Dict = 'd';
  288. public const char Dup = '2';
  289. public const char EmptyDict = '}';
  290. public const char EmptyList = ']';
  291. public const char EmptyTuple = ')';
  292. public const char Ext1 = '\x82';
  293. public const char Ext2 = '\x83';
  294. public const char Ext4 = '\x84';
  295. public const char Float = 'F';
  296. public const char Get = 'g';
  297. public const char Global = 'c';
  298. public const char Inst = 'i';
  299. public const char Int = 'I';
  300. public const char List = 'l';
  301. public const char Long = 'L';
  302. public const char Long1 = '\x8a';
  303. public const char Long4 = '\x8b';
  304. public const char LongBinGet = 'j';
  305. public const char LongBinPut = 'r';
  306. public const char Mark = '(';
  307. public const char NewFalse = '\x89';
  308. public const char NewObj = '\x81';
  309. public const char NewTrue = '\x88';
  310. public const char NoneValue = 'N';
  311. public const char Obj = 'o';
  312. public const char PersId = 'P';
  313. public const char Pop = '0';
  314. public const char PopMark = '1';
  315. public const char Proto = '\x80';
  316. public const char Put = 'p';
  317. public const char Reduce = 'R';
  318. public const char SetItem = 's';
  319. public const char SetItems = 'u';
  320. public const char ShortBinstring = 'U';
  321. public const char Stop = '.';
  322. public const char String = 'S';
  323. public const char Tuple = 't';
  324. public const char Tuple1 = '\x85';
  325. public const char Tuple2 = '\x86';
  326. public const char Tuple3 = '\x87';
  327. public const char Unicode = 'V';
  328. }
  329. #endregion
  330. #region Pickler object
  331. public static PicklerObject/*!*/ Pickler(CodeContext/*!*/ context, [DefaultParameterValue(null)]object file, [DefaultParameterValue(null)]object protocol, [DefaultParameterValue(null)]object bin) {
  332. return new PicklerObject(context, file, protocol, bin);
  333. }
  334. [Documentation("Pickler(file, protocol=0) -> Pickler object\n\n"
  335. + "A Pickler object serializes Python objects to a pickle bytecode stream, which\n"
  336. + "can then be converted back into equivalent objects using an Unpickler.\n"
  337. + "\n"
  338. + "file: an object (such as an open file) that has a write(string) method.\n"
  339. + "protocol: if omitted, protocol 0 is used. If HIGHEST_PROTOCOL or a negative\n"
  340. + " number, the highest available protocol is used.\n"
  341. + "bin: (deprecated; use protocol instead) for backwards compability, a 'bin'\n"
  342. + " keyword parameter is supported. When protocol is specified it is ignored.\n"
  343. + " If protocol is not specified, then protocol 0 is used if bin is false, and\n"
  344. + " protocol 1 is used if bin is true."
  345. )]
  346. [PythonType("Pickler"), PythonHidden]
  347. public class PicklerObject {
  348. private const char LowestPrintableChar = (char)32;
  349. private const char HighestPrintableChar = (char)126;
  350. // max elements that can be set/appended at a time using SETITEMS/APPENDS
  351. private delegate void PickleFunction(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object value);
  352. private static readonly Dictionary<Type, PickleFunction> _dispatchTable;
  353. private int _batchSize = 1000;
  354. private FileOutput _file;
  355. private int _protocol;
  356. private PythonDictionary _memo; // memo if the user accesses the memo property
  357. private Dictionary<object, int> _privMemo; // internal fast memo which we can use if the user doesn't access memo
  358. private object _persist_id;
  359. static PicklerObject() {
  360. _dispatchTable = new Dictionary<Type, PickleFunction>();
  361. _dispatchTable[typeof(PythonDictionary)] = SaveDict;
  362. _dispatchTable[typeof(PythonTuple)] = SaveTuple;
  363. _dispatchTable[typeof(List)] = SaveList;
  364. _dispatchTable[typeof(OldClass)] = SaveGlobal;
  365. _dispatchTable[typeof(PythonFunction)] = SaveGlobal;
  366. _dispatchTable[typeof(BuiltinFunction)] = SaveGlobal;
  367. _dispatchTable[typeof(PythonType)] = SaveGlobal;
  368. _dispatchTable[typeof(OldInstance)] = SaveInstance;
  369. }
  370. #region Public API
  371. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
  372. public PythonDictionary memo {
  373. get {
  374. if (_memo == null) {
  375. // create publicly viewable memo
  376. PythonDictionary resMemo = new PythonDictionary();
  377. foreach (var v in _privMemo) {
  378. resMemo._storage.AddNoLock(
  379. ref resMemo._storage,
  380. Builtin.id(v.Key),
  381. PythonTuple.MakeTuple(v.Value, v.Key)
  382. );
  383. }
  384. _memo = resMemo;
  385. }
  386. return _memo;
  387. }
  388. set {
  389. _memo = value;
  390. _privMemo = null;
  391. }
  392. }
  393. public int proto {
  394. get { return _protocol; }
  395. set { _protocol = value; }
  396. }
  397. public int _BATCHSIZE {
  398. get { return _batchSize; }
  399. set { _batchSize = value; }
  400. }
  401. public object persistent_id {
  402. get {
  403. return _persist_id;
  404. }
  405. set {
  406. _persist_id = value;
  407. }
  408. }
  409. public int binary {
  410. get { return _protocol == 0 ? 1 : 0; }
  411. set { _protocol = value; }
  412. }
  413. public int fast {
  414. // We don't implement fast, but we silently ignore it when it's set so that test_cpickle works.
  415. // For a description of fast, see http://mail.python.org/pipermail/python-bugs-list/2001-October/007695.html
  416. get { return 0; }
  417. set { /* ignore */ }
  418. }
  419. public PicklerObject(CodeContext/*!*/ context, object file, object protocol, object bin) {
  420. int intProtocol;
  421. if (file == null) {
  422. _file = new PythonReadableFileOutput(context, new PythonStringIO.StringO());
  423. } else if (Converter.TryConvertToInt32(file, out intProtocol)) {
  424. // For undocumented (yet tested in official CPython tests) list-based pickler, the
  425. // user could do something like Pickler(1), which would create a protocol-1 pickler
  426. // with an internal string output buffer (retrievable using getvalue()). For a little
  427. // more info, see
  428. // https://sourceforge.net/tracker/?func=detail&atid=105470&aid=939395&group_id=5470
  429. _file = new PythonReadableFileOutput(context, new PythonStringIO.StringO());
  430. protocol = file;
  431. } else if (file is PythonFile) {
  432. _file = new PythonFileOutput((PythonFile)file);
  433. } else if (file is FileOutput) {
  434. _file = (FileOutput)file;
  435. } else {
  436. _file = new PythonFileLikeOutput(context, file);
  437. }
  438. _privMemo = new Dictionary<object, int>(256, ReferenceEqualityComparer.Instance);
  439. if (protocol == null) protocol = PythonOps.IsTrue(bin) ? 1 : 0;
  440. intProtocol = context.LanguageContext.ConvertToInt32(protocol);
  441. if (intProtocol > highestProtocol) {
  442. throw PythonOps.ValueError("pickle protocol {0} asked for; the highest available protocol is {1}", intProtocol, highestProtocol);
  443. } else if (intProtocol < 0) {
  444. this._protocol = highestProtocol;
  445. } else {
  446. this._protocol = intProtocol;
  447. }
  448. }
  449. [Documentation("dump(obj) -> None\n\n"
  450. + "Pickle obj and write the result to the file object that was passed to the\n"
  451. + "constructor\n."
  452. + "\n"
  453. + "Note that you may call dump() multiple times to pickle multiple objects. To\n"
  454. + "unpickle the stream, you will need to call Unpickler's load() method a\n"
  455. + "corresponding number of times.\n"
  456. + "\n"
  457. + "The first time a particular object is encountered, it will be pickled normally.\n"
  458. + "If the object is encountered again (in the same or a later dump() call), a\n"
  459. + "reference to the previously generated value will be pickled. Unpickling will\n"
  460. + "then create multiple references to a single object."
  461. )]
  462. public void dump(CodeContext/*!*/ context, object obj) {
  463. if (_protocol >= 2) WriteProto(context);
  464. Save(context, obj);
  465. Write(context, Opcode.Stop);
  466. }
  467. [Documentation("clear_memo() -> None\n\n"
  468. + "Clear the memo, which is used internally by the pickler to keep track of which\n"
  469. + "objects have already been pickled (so that shared or recursive objects are\n"
  470. + "pickled only once)."
  471. )]
  472. public void clear_memo() {
  473. if (_memo != null) {
  474. _memo.Clear();
  475. } else {
  476. _privMemo.Clear();
  477. }
  478. }
  479. private void Memoize(object obj) {
  480. if (_memo != null) {
  481. if (!MemoContains(PythonOps.Id(obj))) {
  482. _memo[PythonOps.Id(obj)] = PythonTuple.MakeTuple(_memo.Count, obj);
  483. }
  484. } else {
  485. if(!_privMemo.ContainsKey(obj)) {
  486. _privMemo[obj] = _privMemo.Count;
  487. }
  488. }
  489. }
  490. private int MemoizeNew(object obj) {
  491. int res;
  492. if (_memo != null) {
  493. Debug.Assert(!_memo.ContainsKey(obj));
  494. _memo[PythonOps.Id(obj)] = PythonTuple.MakeTuple(res = _memo.Count, obj);
  495. } else {
  496. Debug.Assert(!_privMemo.ContainsKey(obj));
  497. _privMemo[obj] = res = _privMemo.Count;
  498. }
  499. return res;
  500. }
  501. private bool MemoContains(object obj) {
  502. if (_memo != null) {
  503. return _memo.Contains(PythonOps.Id(obj));
  504. }
  505. return _privMemo.ContainsKey(obj);
  506. }
  507. private bool TryWriteFastGet(CodeContext context, object obj) {
  508. int value;
  509. if (_memo != null) {
  510. return TryWriteSlowGet(context, obj);
  511. } else if (_privMemo.TryGetValue(obj, out value)) {
  512. WriteGetOrPut(context, true, value);
  513. return true;
  514. }
  515. return false;
  516. }
  517. private bool TryWriteSlowGet(CodeContext context, object obj) {
  518. object value;
  519. if (_memo.TryGetValue(obj, out value)) {
  520. WriteGetOrPut(context, true, (PythonTuple)value);
  521. return true;
  522. }
  523. return false;
  524. }
  525. [Documentation("getvalue() -> string\n\n"
  526. + "Return the value of the internal string. Raises PicklingError if a file object\n"
  527. + "was passed to this pickler's constructor."
  528. )]
  529. public object getvalue(CodeContext/*!*/ context) {
  530. if (_file is PythonReadableFileOutput) {
  531. return ((PythonReadableFileOutput)_file).GetValue(context);
  532. }
  533. throw PythonExceptions.CreateThrowable(PicklingError(context), "Attempt to getvalue() a non-list-based pickler");
  534. }
  535. #endregion
  536. #region Save functions
  537. private void Save(CodeContext/*!*/ context, object obj) {
  538. if (_persist_id == null || !TrySavePersistId(context, obj)) {
  539. PickleFunction pickleFunction;
  540. // several typees are never memoized, check for these first.
  541. if (obj == null) {
  542. SaveNone(this, context, obj);
  543. } else if (obj is int) {
  544. SaveInteger(this, context, obj);
  545. } else if(obj is BigInteger) {
  546. SaveLong(this, context, obj);
  547. } else if (obj is bool) {
  548. SaveBoolean(this, context, obj);
  549. } else if (obj is double) {
  550. SaveFloat(this, context, obj);
  551. } else if(!TryWriteFastGet(context, obj)) {
  552. if (obj is string) {
  553. // strings are common, specialize them.
  554. SaveUnicode(this, context, obj);
  555. } else {
  556. if (!_dispatchTable.TryGetValue(obj.GetType(), out pickleFunction)) {
  557. if (obj is PythonType) {
  558. // treat classes with metaclasses like regular classes
  559. pickleFunction = SaveGlobal;
  560. } else {
  561. pickleFunction = SaveObject;
  562. }
  563. }
  564. pickleFunction(this, context, obj);
  565. }
  566. }
  567. }
  568. }
  569. private bool TrySavePersistId(CodeContext context, object obj) {
  570. Debug.Assert(_persist_id != null);
  571. string res = Converter.ConvertToString(PythonContext.GetContext(context).CallSplat(_persist_id, obj));
  572. if (res != null) {
  573. SavePersId(context, res);
  574. return true;
  575. }
  576. return false;
  577. }
  578. private void SavePersId(CodeContext/*!*/ context, string res) {
  579. if (this.binary != 0) {
  580. Save(context, res);
  581. Write(context, Opcode.BinPersid);
  582. } else {
  583. Write(context, Opcode.PersId);
  584. Write(context, res);
  585. Write(context, "\n");
  586. }
  587. }
  588. private static void SaveBoolean(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
  589. Debug.Assert(DynamicHelpers.GetPythonType(obj).Equals(TypeCache.Boolean), "arg must be bool");
  590. if (pickler._protocol < 2) {
  591. pickler.Write(context, Opcode.Int);
  592. pickler.Write(context, String.Format("0{0}", ((bool)obj) ? 1 : 0));
  593. pickler.Write(context, Newline);
  594. } else {
  595. if ((bool)obj) {
  596. pickler.Write(context, Opcode.NewTrue);
  597. } else {
  598. pickler.Write(context, Opcode.NewFalse);
  599. }
  600. }
  601. }
  602. private static void SaveDict(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
  603. Debug.Assert(DynamicHelpers.GetPythonType(obj).Equals(TypeCache.Dict), "arg must be dict");
  604. Debug.Assert(!pickler.MemoContains(obj));
  605. int index = pickler.MemoizeNew(obj);
  606. if (pickler._protocol < 1) {
  607. pickler.Write(context, Opcode.Mark);
  608. pickler.Write(context, Opcode.Dict);
  609. } else {
  610. pickler.Write(context, Opcode.EmptyDict);
  611. }
  612. pickler.WritePut(context, index);
  613. pickler.BatchSetItems(context, (PythonDictionary)obj);
  614. }
  615. private static void SaveFloat(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
  616. Debug.Assert(DynamicHelpers.GetPythonType(obj).Equals(TypeCache.Double), "arg must be float");
  617. if (pickler._protocol < 1) {
  618. pickler.Write(context, Opcode.Float);
  619. pickler.WriteFloatAsString(context, obj);
  620. } else {
  621. pickler.Write(context, Opcode.BinFloat);
  622. pickler.WriteFloat64(context, obj);
  623. }
  624. }
  625. private static void SaveGlobal(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
  626. Debug.Assert(
  627. DynamicHelpers.GetPythonType(obj).Equals(TypeCache.OldClass) ||
  628. DynamicHelpers.GetPythonType(obj).Equals(TypeCache.Function) ||
  629. DynamicHelpers.GetPythonType(obj).Equals(TypeCache.BuiltinFunction) ||
  630. DynamicHelpers.GetPythonType(obj).Equals(TypeCache.PythonType) ||
  631. DynamicHelpers.GetPythonType(obj).IsSubclassOf(TypeCache.PythonType),
  632. "arg must be classic class, function, built-in function or method, or new-style type"
  633. );
  634. PythonType pt = obj as PythonType;
  635. if (pt != null) {
  636. pickler.SaveGlobalByName(context, obj, pt.Name);
  637. } else {
  638. object name;
  639. if (PythonOps.TryGetBoundAttr(context, obj, "__name__", out name)) {
  640. pickler.SaveGlobalByName(context, obj, name);
  641. } else {
  642. throw pickler.CannotPickle(context, obj, "could not determine its __name__");
  643. }
  644. }
  645. }
  646. private void SaveGlobalByName(CodeContext/*!*/ context, object obj, object name) {
  647. Debug.Assert(!MemoContains(obj));
  648. object moduleName = FindModuleForGlobal(context, obj, name);
  649. if (_protocol >= 2) {
  650. object code;
  651. if (PythonCopyReg.GetExtensionRegistry(context).TryGetValue(PythonTuple.MakeTuple(moduleName, name), out code)) {
  652. if (IsUInt8(context, code)) {
  653. Write(context, Opcode.Ext1);
  654. WriteUInt8(context, code);
  655. } else if (IsUInt16(context, code)) {
  656. Write(context, Opcode.Ext2);
  657. WriteUInt16(context, code);
  658. } else if (IsInt32(context, code)) {
  659. Write(context, Opcode.Ext4);
  660. WriteInt32(context, code);
  661. } else {
  662. throw PythonOps.RuntimeError("unrecognized integer format");
  663. }
  664. return;
  665. }
  666. }
  667. MemoizeNew(obj);
  668. Write(context, Opcode.Global);
  669. WriteStringPair(context, moduleName, name);
  670. WritePut(context, obj);
  671. }
  672. private static void SaveInstance(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
  673. Debug.Assert(DynamicHelpers.GetPythonType(obj).Equals(TypeCache.OldInstance), "arg must be old-class instance");
  674. Debug.Assert(!pickler.MemoContains(obj));
  675. pickler.Write(context, Opcode.Mark);
  676. // Memoize() call isn't in the usual spot to allow class to be memoized before
  677. // instance (when using proto other than 0) to match CPython's bytecode output
  678. object objClass;
  679. if (!PythonOps.TryGetBoundAttr(context, obj, "__class__", out objClass)) {
  680. throw pickler.CannotPickle(context, obj, "could not determine its __class__");
  681. }
  682. if (pickler._protocol < 1) {
  683. object className, classModuleName;
  684. if (!PythonOps.TryGetBoundAttr(context, objClass, "__name__", out className)) {
  685. throw pickler.CannotPickle(context, obj, "its __class__ has no __name__");
  686. }
  687. classModuleName = pickler.FindModuleForGlobal(context, objClass, className);
  688. Debug.Assert(!pickler.MemoContains(obj));
  689. pickler.MemoizeNew(obj);
  690. pickler.WriteInitArgs(context, obj);
  691. pickler.Write(context, Opcode.Inst);
  692. pickler.WriteStringPair(context, classModuleName, className);
  693. } else {
  694. pickler.Save(context, objClass);
  695. pickler.Memoize(obj);
  696. pickler.WriteInitArgs(context, obj);
  697. pickler.Write(context, Opcode.Obj);
  698. }
  699. pickler.WritePut(context, obj);
  700. object getStateCallable;
  701. if (PythonOps.TryGetBoundAttr(context, obj, "__getstate__", out getStateCallable)) {
  702. pickler.Save(context, PythonCalls.Call(context, getStateCallable));
  703. } else {
  704. pickler.Save(context, PythonOps.GetBoundAttr(context, obj, "__dict__"));
  705. }
  706. pickler.Write(context, Opcode.Build);
  707. }
  708. private static void SaveInteger(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
  709. Debug.Assert(DynamicHelpers.GetPythonType(obj).Equals(TypeCache.Int32), "arg must be int");
  710. if (pickler._protocol < 1) {
  711. pickler.Write(context, Opcode.Int);
  712. pickler.WriteIntAsString(context, obj);
  713. } else {
  714. if (IsUInt8(context, obj)) {
  715. pickler.Write(context, Opcode.BinInt1);
  716. pickler.WriteUInt8(context, obj);
  717. } else if (IsUInt16(context, obj)) {
  718. pickler.Write(context, Opcode.BinInt2);
  719. pickler.WriteUInt16(context, obj);
  720. } else if (IsInt32(context, obj)) {
  721. pickler.Write(context, Opcode.BinInt);
  722. pickler.WriteInt32(context, obj);
  723. } else {
  724. throw PythonOps.RuntimeError("unrecognized integer format");
  725. }
  726. }
  727. }
  728. private static void SaveList(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
  729. Debug.Assert(DynamicHelpers.GetPythonType(obj).Equals(TypeCache.List), "arg must be list");
  730. Debug.Assert(!pickler.MemoContains(obj));
  731. int index = pickler.MemoizeNew(obj);
  732. if (pickler._protocol < 1) {
  733. pickler.Write(context, Opcode.Mark);
  734. pickler.Write(context, Opcode.List);
  735. } else {
  736. pickler.Write(context, Opcode.EmptyList);
  737. }
  738. pickler.WritePut(context, index);
  739. pickler.BatchAppends(context, ((IEnumerable)obj).GetEnumerator());
  740. }
  741. #if CLR2
  742. private static readonly BigInteger MaxInt = BigInteger.Create(Int32.MaxValue);
  743. private static readonly BigInteger MinInt = BigInteger.Create(Int32.MinValue);
  744. #else
  745. private static readonly BigInteger MaxInt = new BigInteger(Int32.MaxValue);
  746. private static readonly BigInteger MinInt = new BigInteger(Int32.MinValue);
  747. #endif
  748. private static void SaveLong(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
  749. Debug.Assert(DynamicHelpers.GetPythonType(obj).Equals(TypeCache.BigInteger), "arg must be long");
  750. BigInteger bi = (BigInteger)obj;
  751. if (pickler._protocol < 2) {
  752. pickler.Write(context, Opcode.Long);
  753. pickler.WriteLongAsString(context, obj);
  754. } else if (bi.IsZero()) {
  755. pickler.Write(context, Opcode.Long1);
  756. pickler.WriteUInt8(context, 0);
  757. } else if (bi <= MaxInt && bi >= MinInt) {
  758. pickler.Write(context, Opcode.Long1);
  759. int value = (int)bi;
  760. if (IsInt8(value)) {
  761. pickler.WriteUInt8(context, 1);
  762. pickler._file.Write(context, (char)(byte)value);
  763. } else if (IsInt16(value)) {
  764. pickler.WriteUInt8(context, 2);
  765. pickler.WriteUInt8(context, value & 0xff);
  766. pickler.WriteUInt8(context, (value >> 8) & 0xff);
  767. } else {
  768. pickler.WriteUInt8(context, 4);
  769. pickler.WriteInt32(context, value);
  770. }
  771. } else {
  772. byte[] dataBytes = bi.ToByteArray();
  773. if (dataBytes.Length < 256) {
  774. pickler.Write(context, Opcode.Long1);
  775. pickler.WriteUInt8(context, dataBytes.Length);
  776. } else {
  777. pickler.Write(context, Opcode.Long4);
  778. pickler.WriteInt32(context, dataBytes.Length);
  779. }
  780. foreach (byte b in dataBytes) {
  781. pickler.WriteUInt8(context, b);
  782. }
  783. }
  784. }
  785. private static void SaveNone(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
  786. Debug.Assert(DynamicHelpers.GetPythonType(obj).Equals(TypeCache.Null), "arg must be None");
  787. pickler.Write(context, Opcode.NoneValue);
  788. }
  789. /// <summary>
  790. /// Call the appropriate reduce method for obj and pickle the object using
  791. /// the resulting data. Use the first available of
  792. /// copy_reg.dispatch_table[type(obj)], obj.__reduce_ex__, and obj.__reduce__.
  793. /// </summary>
  794. private void SaveObject(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
  795. Debug.Assert(!MemoContains(obj));
  796. MemoizeNew(obj);
  797. object reduceCallable, result;
  798. PythonType objType = DynamicHelpers.GetPythonType(obj);
  799. if (((IDictionary<object, object>)PythonCopyReg.GetDispatchTable(context)).TryGetValue(objType, out reduceCallable)) {
  800. result = PythonCalls.Call(context, reduceCallable, obj);
  801. } else if (PythonOps.TryGetBoundAttr(context, obj, "__reduce_ex__", out reduceCallable)) {
  802. if (obj is PythonType) {
  803. result = context.LanguageContext.Call(context, reduceCallable, obj, _protocol);
  804. } else {
  805. result = context.LanguageContext.Call(context, reduceCallable, _protocol);
  806. }
  807. } else if (PythonOps.TryGetBoundAttr(context, obj, "__reduce__", out reduceCallable)) {
  808. if (obj is PythonType) {
  809. result = context.LanguageContext.Call(context, reduceCallable, obj);
  810. } else {
  811. result = context.LanguageContext.Call(context, reduceCallable);
  812. }
  813. } else {
  814. throw PythonOps.AttributeError("no reduce function found for {0}", obj);
  815. }
  816. if (objType.Equals(TypeCache.String)) {
  817. if (!TryWriteFastGet(context, obj)) {
  818. SaveGlobalByName(context, obj, result);
  819. }
  820. } else if (result is PythonTuple) {
  821. PythonTuple rt = (PythonTuple)result;
  822. switch (rt.__len__()) {
  823. case 2:
  824. SaveReduce(context, obj, reduceCallable, rt[0], rt[1], null, null, null);
  825. break;
  826. case 3:
  827. SaveReduce(context, obj, reduceCallable, rt[0], rt[1], rt[2], null, null);
  828. break;
  829. case 4:
  830. SaveReduce(context, obj, reduceCallable, rt[0], rt[1], rt[2], rt[3], null);
  831. break;
  832. case 5:
  833. SaveReduce(context, obj, reduceCallable, rt[0], rt[1], rt[2], rt[3], rt[4]);
  834. break;
  835. default:
  836. throw CannotPickle(context, obj, "tuple returned by {0} must have to to five elements", reduceCallable);
  837. }
  838. } else {
  839. throw CannotPickle(context, obj, "{0} must return string or tuple", reduceCallable);
  840. }
  841. }
  842. /// <summary>
  843. /// Pickle the result of a reduce function.
  844. ///
  845. /// Only context, obj, func, and reduceCallable are required; all other arguments may be null.
  846. /// </summary>
  847. private void SaveReduce(CodeContext/*!*/ context, object obj, object reduceCallable, object func, object args, object state, object listItems, object dictItems) {
  848. if (!PythonOps.IsCallable(context, func)) {
  849. throw CannotPickle(context, obj, "func from reduce() should be callable");
  850. } else if (!(args is PythonTuple) && args != null) {
  851. throw CannotPickle(context, obj, "args from reduce() should be a tuple");
  852. } else if (listItems != null && !(listItems is IEnumerator)) {
  853. throw CannotPickle(context, obj, "listitems from reduce() should be a list iterator");
  854. } else if (dictItems != null && !(dictItems is IEnumerator)) {
  855. throw CannotPickle(context, obj, "dictitems from reduce() should be a dict iterator");
  856. }
  857. object funcName;
  858. string funcNameString;
  859. if (func is PythonType) {
  860. funcNameString = ((PythonType)func).Name;
  861. } else {
  862. if (!PythonOps.TryGetBoundAttr(context, func, "__name__", out funcName)) {
  863. throw CannotPickle(context, obj, "func from reduce() ({0}) should have a __name__ attribute");
  864. } else if (!Converter.TryConvertToString(funcName, out funcNameString) || funcNameString == null) {
  865. throw CannotPickle(context, obj, "__name__ of func from reduce() must be string");
  866. }
  867. }
  868. if (_protocol >= 2 && "__newobj__" == funcNameString) {
  869. if (args == null) {
  870. throw CannotPickle(context, obj, "__newobj__ arglist is None");
  871. }
  872. PythonTuple argsTuple = (PythonTuple)args;
  873. if (argsTuple.__len__() == 0) {
  874. throw CannotPickle(context, obj, "__newobj__ arglist is empty");
  875. } else if (!DynamicHelpers.GetPythonType(obj).Equals(argsTuple[0])) {
  876. throw CannotPickle(context, obj, "args[0] from __newobj__ args has the wrong class");
  877. }
  878. Save(context, argsTuple[0]);
  879. Save(context, argsTuple[new Slice(1, null)]);
  880. Write(context, Opcode.NewObj);
  881. } else {
  882. Save(context, func);
  883. Save(context, args);
  884. Write(context, Opcode.Reduce);
  885. }
  886. WritePut(context, obj);
  887. if (state != null) {
  888. Save(context, state);
  889. Write(context, Opcode.Build);
  890. }
  891. if (listItems != null) {
  892. BatchAppends(context, (IEnumerator)listItems);
  893. }
  894. if (dictItems != null) {
  895. BatchSetItems(context, (IEnumerator)dictItems);
  896. }
  897. }
  898. private static void SaveTuple(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
  899. Debug.Assert(DynamicHelpers.GetPythonType(obj).Equals(TypeCache.PythonTuple), "arg must be tuple");
  900. Debug.Assert(!pickler.MemoContains(obj));
  901. PythonTuple t = (PythonTuple)obj;
  902. char opcode;
  903. bool needMark = false;
  904. int len = t._data.Length;
  905. if (pickler._protocol > 0 && len == 0) {
  906. opcode = Opcode.EmptyTuple;
  907. } else if (pickler._protocol >= 2 && len == 1) {
  908. opcode = Opcode.Tuple1;
  909. } else if (pickler._protocol >= 2 && len == 2) {
  910. opcode = Opcode.Tuple2;
  911. } else if (pickler._protocol >= 2 && len == 3) {
  912. opcode = Opcode.Tuple3;
  913. } else {
  914. opcode = Opcode.Tuple;
  915. needMark = true;
  916. }
  917. if (needMark) pickler.Write(context, Opcode.Mark);
  918. var data = t._data;
  919. for (int i = 0; i < data.Length; i++) {
  920. pickler.Save(context, data[i]);
  921. }
  922. if (len > 0) {
  923. if (pickler.MemoContains(obj)) {
  924. // recursive tuple
  925. if (pickler._protocol == 1) {
  926. pickler.Write(context, Opcode.PopMark);
  927. } else {
  928. if (pickler._protocol == 0) {
  929. pickler.Write(context, Opcode.Pop);
  930. }
  931. for (int i = 0; i < len; i++) {
  932. pickler.Write(context, Opcode.Pop);
  933. }
  934. }
  935. pickler.WriteGet(context, obj);
  936. return;
  937. }
  938. pickler.Write(context, opcode);
  939. pickler.Memoize(t);
  940. pickler.WritePut(context, t);
  941. } else {
  942. pickler.Write(context, opcode);
  943. }
  944. }
  945. private static void SaveUnicode(PicklerObject/*!*/ pickler, CodeContext/*!*/ context, object obj) {
  946. Debug.Assert(DynamicHelpers.GetPythonType(obj).Equals(TypeCache.String), "arg must be unicode");
  947. Debug.Assert(!pickler.MemoContains(obj));
  948. if (pickler._memo != null) {
  949. pickler.MemoizeNew(obj);
  950. if (pickler._protocol < 1) {
  951. pickler.Write(context, Opcode.Unicode);
  952. pickler.WriteUnicodeStringRaw(context, obj);
  953. } else {
  954. pickler.Write(context, Opcode.BinUnicode);
  955. pickler.WriteUnicodeStringUtf8(context, obj);
  956. }
  957. pickler.WritePut(context, obj);
  958. } else {
  959. var memo = pickler._privMemo[obj] = pickler._privMemo.Count;
  960. if (pickler._protocol < 1) {
  961. pickler.Write(context, Opcode.Unicode);
  962. pickler.WriteUnicodeStringRaw(context, obj);
  963. } else {
  964. pickler.Write(context, Opcode.BinUnicode);
  965. pickler.WriteUnicodeStringUtf8(context, obj);
  966. }
  967. pickler.WriteGetOrPut(context, false, memo);
  968. }
  969. }
  970. #endregion
  971. #region Output encoding
  972. /// <summary>
  973. /// Write value in pickle decimalnl_short format.
  974. /// </summary>
  975. private void WriteFloatAsString(CodeContext/*!*/ context, object value) {
  976. Debug.Assert(DynamicHelpers.GetPythonType(value).Equals(TypeCache.Double));
  977. Write(context, DoubleOps.__repr__(context, (double)value));
  978. Write(context, Newline);
  979. }
  980. /// <summary>
  981. /// Write value in pickle float8 format.
  982. /// </summary>
  983. private void WriteFloat64(CodeContext/*!*/ context, object value) {
  984. Debug.Assert(DynamicHelpers.GetPythonType(value).Equals(TypeCache.Double));
  985. Write(context, _float64.pack(context, value));
  986. }
  987. /// <summary>
  988. /// Write value in pickle uint1 format.
  989. /// </summary>
  990. private void WriteUInt8(CodeContext/*!*/ context, object value) {
  991. Debug.Assert(IsUInt8(context, value));
  992. if (value is int) {
  993. Write(context, ScriptingRuntimeHelpers.CharToString((char)(int)(value)));
  994. } else if (value is BigInteger) {
  995. Write(context, ScriptingRuntimeHelpers.CharToString((char)(int)(BigInteger)(value)));
  996. } else if (value is byte) {
  997. // TODO: Shouldn't be here
  998. Write(context, ScriptingRuntimeHelpers.CharToString((char)(byte)(value)));
  999. } else {
  1000. throw Assert.Unreachable;
  1001. }
  1002. }
  1003. private void WriteUInt8(CodeContext/*!*/ context, int value) {
  1004. _file.Write(context, (char)value);
  1005. }
  1006. /// <summary>
  1007. /// Write value in pickle uint2 format.
  1008. /// </summary>
  1009. private void WriteUInt16(CodeContext/*!*/ context, object value) {
  1010. Debug.Assert(IsUInt16(context, value));
  1011. int iVal = (int)value;
  1012. WriteUInt8(context, iVal & 0xff);
  1013. WriteUInt8(context, (iVal >> 8) & 0xff);
  1014. }
  1015. /// <summary>
  1016. /// Write value in pickle int4 format.
  1017. /// </summary>
  1018. private void WriteInt32(CodeContext/*!*/ context, object value) {
  1019. Debug.Assert(IsInt32(context, value));
  1020. int val = (int)value;
  1021. WriteInt32(context, val);
  1022. }
  1023. private void WriteInt32(CodeContext context, int val) {
  1024. _file.Write(context, val);
  1025. }
  1026. /// <summary>
  1027. /// Write value in pickle decimalnl_short format.
  1028. /// </summary>
  1029. private void WriteIntAsString(CodeContext/*!*/ context, object value) {
  1030. Debug.Assert(IsInt32(context, value));
  1031. Write(context, PythonOps.Repr(context, value));
  1032. Write(context, Newline);
  1033. }
  1034. /// <summary>
  1035. /// Write value in pickle decimalnl_short format.
  1036. /// </summary>
  1037. private void WriteIntAsString(CodeContext/*!*/ context, int value) {
  1038. Write(context, value.ToString());
  1039. Write(context, Newline);
  1040. }
  1041. /// <summary>
  1042. /// Write value in pickle decimalnl_long format.
  1043. /// </summary>
  1044. private void WriteLongAsString(CodeContext/*!*/ context, object value) {
  1045. Debug.Assert(DynamicHelpers.GetPythonType(value).Equals(TypeCache.BigInteger));
  1046. Write(context, PythonOps.Repr(context, value));
  1047. Write(context, Newline);
  1048. }
  1049. /// <summary>
  1050. /// Write value in pickle unicodestringnl format.
  1051. /// </summary>
  1052. private void WriteUnicodeStringRaw(CodeContext/*!*/ context, object value) {
  1053. Debug.Assert(DynamicHelpers.GetPythonType(value).Equals(TypeCache.String));
  1054. // manually escape backslash and newline
  1055. Write(context, StringOps.RawUnicodeEscapeEncode(((string)value).Replace("\\", "\\u005c").Replace("\n", "\\u000a")));
  1056. Write(context, Newline);
  1057. }
  1058. /// <summary>
  1059. /// Write value in pickle unicodestring4 format.
  1060. /// </summary>
  1061. private void WriteUnicodeStringUtf8(CodeContext/*!*/ context, object value) {
  1062. Debug.Assert(DynamicHelpers.GetPythonType(value).Equals(TypeCache.String));
  1063. string strVal = (string)value;
  1064. // if the string contains non-ASCII elements it needs to be re-encoded as UTF8.
  1065. for (int i = 0; i < strVal.Length; i++) {
  1066. if (strVal[i] >= 128) {
  1067. string encodedString = System.Text.Encoding.UTF8.GetBytes((string)value).MakeString();
  1068. WriteInt32(context, encodedString.Length);
  1069. Write(context, encodedString);
  1070. return;
  1071. }
  1072. }
  1073. WriteInt32(context, strVal.Length);
  1074. Write(context, strVal);
  1075. }
  1076. /// <summary>
  1077. /// Write value in pickle stringnl_noescape_pair format.
  1078. /// </summary>
  1079. private void WriteStringPair(CodeContext/*!*/ context, object value1, object value2) {
  1080. Debug.Assert(DynamicHelpers.GetPythonType(value1).Equals(TypeCache.String));
  1081. Debug.Assert(DynamicHelpers.GetPythonType(value2).Equals(TypeCache.String));
  1082. #if DEBUG
  1083. Debug.Assert(IsPrintableAscii(value1));
  1084. Debug.Assert(IsPrintableAscii(value2));
  1085. #endif
  1086. Write(context, (string)value1);
  1087. Write(context, Newline);
  1088. Write(context, (string)value2);
  1089. Write(context, Newline);
  1090. }
  1091. #endregion
  1092. #region Type checking
  1093. /// <summary>
  1094. /// Return true if value is appropriate for formatting in pickle uint1 format.
  1095. /// </summary>
  1096. private static bool IsUInt8(CodeContext/*!*/ context, object value) {
  1097. if (value is int) {
  1098. return IsUInt8((int)value);
  1099. }
  1100. PythonContext pc = PythonContext.GetContext(context);
  1101. return pc.LessThanOrEqual(0, value) && pc.LessThan(value, 1 << 8);
  1102. }
  1103. private static bool IsUInt8(int value) {
  1104. return (value >= 0 && value < 1 << 8);
  1105. }
  1106. private static bool IsInt8(int value) {
  1107. return (value >= SByte.MinValue && value <= SByte.MaxValue);
  1108. }
  1109. /// <summary>
  1110. /// Return true if value is appropriate for formatting in pickle uint2 format.
  1111. /// </summary>
  1112. private static bool IsUInt16(CodeContext/*!*/ context, object value) {
  1113. if (value is int) {
  1114. return IsUInt16((int)value);
  1115. }
  1116. PythonContext pc = PythonContext.GetContext(context);
  1117. return pc.LessThanOrEqual(1 << 8, value) && pc.LessThan(value, 1 << 16);
  1118. }
  1119. private static bool IsUInt16(int value) {
  1120. return (value >= 0 && value < 1 << 16);
  1121. }
  1122. private static bool IsInt16(int value) {
  1123. return (value >= short.MinValue && value <= short.MaxValue);
  1124. }
  1125. /// <summary>
  1126. /// Return true if value is appropriate for formatting in pickle int4 format.
  1127. /// </summary>
  1128. private static bool IsInt32(CodeContext/*!*/ context, object value) {
  1129. PythonContext pc = PythonContext.GetContext(context);
  1130. return pc.LessThanOrEqual(Int32.MinValue, value) && pc.LessThanOrEqual(value, Int32.MaxValue);
  1131. }
  1132. #if DEBUG
  1133. /// <summary>
  1134. /// Return true if value is a string where each value is in the range of printable ASCII characters.
  1135. /// </summary>
  1136. private bool IsPrintableAscii(object value) {
  1137. Debug.Assert(DynamicHelpers.GetPythonType(value).Equals(TypeCache.String));
  1138. string strValue = (string)value;
  1139. foreach (char c in strValue) {
  1140. if (!(LowestPrintableChar <= c && c <= HighestPrintableChar)) return false;
  1141. }
  1142. return true;
  1143. }
  1144. #endif
  1145. #endregion
  1146. #region Output generation helpers
  1147. private void Write(CodeContext/*!*/ context, string data) {
  1148. _file.Write(context, data);
  1149. }
  1150. private void Write(CodeContext/*!*/ context, char data) {
  1151. _file.Write(context, data);
  1152. }
  1153. private void WriteGet(CodeContext/*!*/ context, object obj) {
  1154. Debug.Assert(MemoContains(obj));
  1155. WriteGetOrPut(context, obj, true);
  1156. }
  1157. private void WriteGetOrPut(CodeContext context, object obj, bool isGet) {
  1158. Debug.Assert(MemoContains(obj));
  1159. if (_memo == null) {
  1160. WriteGetOrPut(context, isGet, _privMemo[obj]);
  1161. } else {
  1162. WriteGetOrPut(context, isGet, (PythonTuple)_memo[PythonOps.Id(obj)]);
  1163. }
  1164. }
  1165. private void WriteGetOrPut(CodeContext context, bool isGet, PythonTuple tup) {
  1166. object index = tup[0];
  1167. Debug.Assert(PythonContext.GetContext(context).GreaterThanOrEqual(index, 0));
  1168. if (_protocol < 1) {
  1169. Write(context, isGet ? Opcode.Get : Opcode.Put);
  1170. WriteIntAsString(context, index);
  1171. } else {
  1172. if (IsUInt8(context, index)) {
  1173. Write(context, isGet ? Opcode.BinGet : Opcode.BinPut);
  1174. WriteUInt8(context, index);
  1175. } else {
  1176. Write(context, isGet ? Opcode.LongBinGet : Opcode.LongBinPut);
  1177. WriteInt32(context, index);
  1178. }
  1179. }
  1180. }
  1181. private void WriteGetOrPut(CodeContext context, bool isGet, int index) {
  1182. if (_protocol < 1) {
  1183. Write(context, isGet ? Opcode.Get : Opcode.Put);
  1184. WriteIntAsString(context, index);
  1185. } else if(index >= 0 && index <= 1 << 8) {
  1186. Write(context, isGet ? Opcode.BinGet : Opcode.BinPut);
  1187. WriteUInt8(context, index);
  1188. } else {
  1189. Write(context, isGet ? Opcode.LongBinGet : Opcode.LongBinPut);
  1190. WriteInt32(context, index);
  1191. }
  1192. }
  1193. private void WriteInitArgs(CodeContext/*!*/ context, object obj) {
  1194. object getInitArgsCallable;
  1195. if (PythonOps.TryGetBoundAttr(context, obj, "__getinitargs__", out getInitArgsCallable)) {
  1196. object initArgs = PythonCalls.Call(context, getInitArgsCallable);
  1197. if (!(initArgs is PythonTuple)) {
  1198. throw CannotPickle(context, obj, "__getinitargs__() must return tuple");
  1199. }
  1200. foreach (object arg in (PythonTuple)initArgs) {
  1201. Save(context, arg);
  1202. }
  1203. }
  1204. }
  1205. private void WritePut(CodeContext/*!*/ context, object obj) {
  1206. WriteGetOrPut(context, obj, false);
  1207. }
  1208. private void WritePut(CodeContext/*!*/ context, int index) {
  1209. WriteGetOrPut(context, false, index);
  1210. }
  1211. private void WriteProto(CodeContext/*!*/ context) {
  1212. Write(context, Opcode.Proto);
  1213. WriteUInt8(context, _protocol);
  1214. }
  1215. /// <summary>
  1216. /// Emit a series of opcodes that will set append all items indexed by iter
  1217. /// to the object at the top of the stack. Use APPENDS if possible, but
  1218. /// append no more than BatchSize items at a time.
  1219. /// </summary>
  1220. private void BatchAppends(CodeContext/*!*/ context, IEnumerator enumerator) {
  1221. if (_protocol < 1) {
  1222. while (enumerator.MoveNext()) {
  1223. Save(context, enumerator.Current);
  1224. Write(context, Opcode.Append);
  1225. }
  1226. } else {
  1227. object next;
  1228. if (enumerator.MoveNext()) {
  1229. next = enumerator.Current;
  1230. } else {
  1231. return;
  1232. }
  1233. int batchCompleted = 0;
  1234. object current;
  1235. // We do a one-item lookahead to avoid emitting an APPENDS for a
  1236. // single remaining item.
  1237. while (enumerator.MoveNext()) {
  1238. current = next;
  1239. next = enumerator.Current;
  1240. if (batchCompleted == _BATCHSIZE) {
  1241. Write(context, Opcode.Appends);
  1242. batchCompleted = 0;
  1243. }
  1244. if (batchCompleted == 0) {
  1245. Write(context, Opcode.Mark);
  1246. }
  1247. Save(context, current);
  1248. batchCompleted++;
  1249. }
  1250. if (batchCompleted == _BATCHSIZE) {
  1251. Write(context, Opcode.Appends);
  1252. batchCompleted = 0;
  1253. }
  1254. Save(context, next);
  1255. batchCompleted++;
  1256. if (batchCompleted > 1) {
  1257. Write(context, Opcode.Appends);
  1258. } else {
  1259. Write(context, Opcode.Append);
  1260. }
  1261. }
  1262. }
  1263. /// <summary>
  1264. /// Emit a series of opcodes that will set all (key, value) pairs indexed by
  1265. /// iter in the object at the top of the stack. Use SETITEMS if possible,
  1266. /// but append no more than BatchSize items at a time.
  1267. /// </summary>
  1268. private void BatchSetItems(CodeContext/*!*/ context, PythonDictionary dict) {
  1269. KeyValuePair<object, object> kvTuple;
  1270. using (var enumerator = dict._storage.GetEnumerator()) {
  1271. if (_protocol < 1) {
  1272. while (enumerator.MoveNext()) {
  1273. kvTuple = enumerator.Current;
  1274. Save(context, kvTuple.Key);
  1275. Save(context, kvTuple.Value);
  1276. Write(context, Opcode.SetItem);
  1277. }
  1278. } else {
  1279. object nextKey, nextValue;
  1280. if (enumerator.MoveNext()) {
  1281. kvTuple = enumerator.Current;
  1282. nextKey = kvTuple.Key;
  1283. nextValue = kvTuple.Value;
  1284. } else {
  1285. return;
  1286. }
  1287. int batchCompleted = 0;
  1288. object curKey, curValue;
  1289. // We do a one-item lookahead to avoid emitting a SETITEMS for a
  1290. // single remaining item.
  1291. while (enumerator.MoveNext()) {
  1292. curKey = nextKey;
  1293. curValue = nextValue;
  1294. kvTuple = enumerator.Current;
  1295. nextKey = kvTuple.Key;
  1296. nextValue = kvTuple.Value;
  1297. if (batchCompleted == _BATCHSIZE) {
  1298. Write(context, Opcode.SetItems);
  1299. batchCompleted = 0;
  1300. }
  1301. if (batchCompleted == 0) {
  1302. Write(context, Opcode.Mark);
  1303. }
  1304. Save(context, curKey);
  1305. Save(context, curValue);
  1306. batchCompleted++;
  1307. }
  1308. if (batchCompleted == _BATCHSIZE) {
  1309. Write(context, Opcode.SetItems);
  1310. batchCompleted = 0;
  1311. }
  1312. Save(context, nextKey);
  1313. Save(context, nextValue);
  1314. batchCompleted++;
  1315. if (batchCompleted > 1) {
  1316. Write(context, Opcode.SetItems);
  1317. } else {
  1318. Write(context, Opcode.SetItem);
  1319. }
  1320. }
  1321. }
  1322. }
  1323. /// <summary>
  1324. /// Emit a series of opcodes that will set all (key, value) pairs indexed by
  1325. /// iter in the object at the top of the stack. Use SETITEMS if possible,
  1326. /// but append no more than BatchSize items at a time.
  1327. /// </summary>
  1328. private void BatchSetItems(CodeContext/*!*/ context, IEnumerator enumerator) {
  1329. PythonTuple kvTuple;
  1330. if (_protocol < 1) {
  1331. while (enumerator.MoveNext()) {
  1332. kvTuple = (PythonTuple)enumerator.Current;
  1333. Save(context, kvTuple[0]);
  1334. Save(context, kvTuple[1]);
  1335. Write(context, Opcode.SetItem);
  1336. }
  1337. } else {
  1338. object nextKey, nextValue;
  1339. if (enumerator.MoveNext()) {
  1340. kvTuple = (PythonTuple)enumerator.Current;
  1341. nextKey = kvTuple[0];
  1342. nextValue = kvTuple[1];
  1343. } else {
  1344. return;
  1345. }
  1346. int batchCompleted = 0;
  1347. object curKey, curValue;
  1348. // We do a one-item lookahead to avoid emitting a SETITEMS for a
  1349. // single remaining item.
  1350. while (enumerator.MoveNext()) {
  1351. curKey = nextKey;
  1352. curValue = nextValue;
  1353. kvTuple = (PythonTuple)enumerator.Current;
  1354. nextKey = kvTuple[0];
  1355. nextValue = kvTuple[1];
  1356. if (batchCompleted == _BATCHSIZE) {
  1357. Write(context, Opcode.SetItems);
  1358. batchCompleted = 0;
  1359. }
  1360. if (batchCompleted == 0) {
  1361. Write(context, Opcode.Mark);
  1362. }
  1363. Save(context, curKey);
  1364. Save(context, curValue);
  1365. batchCompleted++;
  1366. }
  1367. if (batchCompleted == _BATCHSIZE) {
  1368. Write(context, Opcode.SetItems);
  1369. batchCompleted = 0;
  1370. }
  1371. Save(context, nextKey);
  1372. Save(context, nextValue);
  1373. batchCompleted++;
  1374. if (batchCompleted > 1) {
  1375. Write(context, Opcode.SetItems);
  1376. } else {
  1377. Write(context, Opcode.SetItem);
  1378. }
  1379. }
  1380. }
  1381. #endregion
  1382. #region Other private helper methods
  1383. private Exception CannotPickle(CodeContext/*!*/ context, object obj, string format, params object[] args) {
  1384. StringBuilder msgBuilder = new StringBuilder();
  1385. msgBuilder.Append("Can't pickle ");
  1386. msgBuilder.Append(PythonOps.ToString(context, obj));
  1387. if (format != null) {
  1388. msgBuilder.Append(": ");
  1389. msgBuilder.Append(String.Format(format, args));
  1390. }
  1391. return PythonExceptions.CreateThrowable(PickleError(context), msgBuilder.ToString());
  1392. }
  1393. /// <summary>
  1394. /// Find the module for obj and ensure that obj is reachable in that module by the given name.
  1395. ///
  1396. /// Throw PicklingError if any of the following are true:
  1397. /// - The module couldn't be determined.
  1398. /// - The module couldn't be loaded.
  1399. /// - The given name doesn't exist in the module.
  1400. /// - The given name is a different object than obj.
  1401. ///
  1402. /// Otherwise, return the name of the module.
  1403. ///
  1404. /// To determine which module obj lives in, obj.__module__ is used if available. The
  1405. /// module named by obj.__module__ is loaded if needed. If obj has no __module__
  1406. /// attribute, then each loaded module is searched. If a loaded module has an
  1407. /// attribute with the given name, and that attribute is the same object as obj,
  1408. /// then that module is used.
  1409. /// </summary>
  1410. private object FindModuleForGlobal(CodeContext/*!*/ context, object obj, object name) {
  1411. object module;
  1412. object moduleName;
  1413. PythonType pt = obj as PythonType;
  1414. if (pt != null) {
  1415. return PythonType.Get__module__(context, pt);
  1416. } else if (PythonOps.TryGetBoundAttr(context, obj, "__module__", out moduleName)) {
  1417. // TODO: Global SystemState
  1418. LightExceptions.CheckAndThrow(Builtin.__import__(context, Converter.ConvertToString(moduleName)));
  1419. object foundObj;
  1420. if (Importer.TryGetExistingModule(context, Converter.ConvertToString(moduleName), out module) &&
  1421. PythonOps.TryGetBoundAttr(context, module, Converter.ConvertToString(name), out foundObj)) {
  1422. if (PythonOps.IsRetBool(foundObj, obj)) {
  1423. return moduleName;
  1424. } else {
  1425. throw CannotPickle(context, obj, "it's not the same object as {0}.{1}", moduleName, name);
  1426. }
  1427. } else {
  1428. throw CannotPickle(context, obj, "it's not found as {0}.{1}", moduleName, name);
  1429. }
  1430. } else {
  1431. // No obj.__module__, so crawl through all loaded modules looking for obj
  1432. foreach (KeyValuePair<object, object> modulePair in context.LanguageContext.SystemStateModules) {
  1433. moduleName = modulePair.Key;
  1434. module = modulePair.Value;
  1435. object foundObj;
  1436. if (PythonOps.TryGetBoundAttr(context, module, Converter.ConvertToString(name), out foundObj) &&
  1437. PythonOps.IsRetBool(foundObj, obj)
  1438. ) {
  1439. return moduleName;
  1440. }
  1441. }
  1442. throw CannotPickle(context, obj, "could not determine its module");
  1443. }
  1444. }
  1445. #endregion
  1446. }
  1447. #endregion
  1448. #region Unpickler object
  1449. public static UnpicklerObject Unpickler(CodeContext/*!*/ context, object file) {
  1450. return new UnpicklerObject(context, file);
  1451. }
  1452. [Documentation("Unpickler(file) -> Unpickler object\n\n"
  1453. + "An Unpickler object reads a pickle bytecode stream and creates corresponding\n"
  1454. + "objects."
  1455. + "\n"
  1456. + "file: an object (such as an open file or a StringIO) with read(num_chars) and\n"
  1457. + " readline() methods that return strings"
  1458. )]
  1459. [PythonType("Unpickler"), PythonHidden]
  1460. public class UnpicklerObject {
  1461. private static readonly object _mark = new object();
  1462. private FileInput _file;
  1463. private List<object> _stack;
  1464. private PythonDictionary _memo;
  1465. private List<object> _privMemo;
  1466. private object _pers_loader;
  1467. public UnpicklerObject() {
  1468. _privMemo = new List<object>(200);
  1469. }
  1470. public UnpicklerObject(CodeContext context, object file)
  1471. : this() {
  1472. _file = new PythonFileInput(context, file);
  1473. }
  1474. internal UnpicklerObject(CodeContext context, FileInput input)
  1475. : this() {
  1476. _file = input;
  1477. }
  1478. [Documentation("load() -> unpickled object\n\n"
  1479. + "Read pickle data from the file object that was passed to the constructor and\n"
  1480. + "return the corresponding unpickled objects."
  1481. )]
  1482. public object load(CodeContext/*!*/ context) {
  1483. _stack = new List<object>(32);
  1484. for (; ; ) {
  1485. var opcode = _file.ReadChar(context);
  1486. switch (opcode) {
  1487. case Opcode.Append: LoadAppend(context); break;
  1488. case Opcode.Appends: LoadAppends(context); break;
  1489. case Opcode.BinFloat: LoadBinFloat(context); break;
  1490. case Opcode.BinGet: LoadBinGet(context); break;
  1491. case Opcode.BinInt: LoadBinInt(context); break;
  1492. case Opcode.BinInt1: LoadBinInt1(context); break;
  1493. case Opcode.BinInt2: LoadBinInt2(context); break;
  1494. case Opcode.BinPersid: LoadBinPersid(context); break;
  1495. case Opcode.BinPut: LoadBinPut(context); break;
  1496. case Opcode.BinString: LoadBinString(context); break;
  1497. case Opcode.BinUnicode: LoadBinUnicode(context); break;
  1498. case Opcode.Build: LoadBuild(context); break;
  1499. case Opcode.Dict: LoadDict(context); break;
  1500. case Opcode.Dup: LoadDup(context); break;
  1501. case Opcode.EmptyDict: LoadEmptyDict(context); break;
  1502. case Opcode.EmptyList: LoadEmptyList(context); break;
  1503. case Opcode.EmptyTuple: LoadEmptyTuple(context); break;
  1504. case Opcode.Ext1: LoadExt1(context); break;
  1505. case Opcode.Ext2: LoadExt2(context); break;
  1506. case Opcode.Ext4: LoadExt4(context); break;
  1507. case Opcode.Float: LoadFloat(context); break;
  1508. case Opcode.Get: LoadGet(context); break;
  1509. case Opcode.Global: LoadGlobal(context); break;
  1510. case Opcode.Inst: LoadInst(context); break;
  1511. case Opcode.Int: LoadInt(context); break;
  1512. case Opcode.List: LoadList(context); break;
  1513. case Opcode.Long: LoadLong(context); break;
  1514. case Opcode.Long1: LoadLong1(context); break;
  1515. case Opcode.Long4: LoadLong4(context); break;
  1516. case Opcode.LongBinGet: LoadLongBinGet(context); break;
  1517. case Opcode.LongBinPut: LoadLongBinPut(context); break;
  1518. case Opcode.Mark: LoadMark(context); break;
  1519. case Opcode.NewFalse: LoadNewFalse(context); break;
  1520. case Opcode.NewObj: LoadNewObj(context); break;
  1521. case Opcode.NewTrue: LoadNewTrue(context); break;
  1522. case Opcode.NoneValue: LoadNoneValue(context); break;
  1523. case Opcode.Obj: LoadObj(context); break;
  1524. case Opcode.PersId: LoadPersId(context); break;
  1525. case Opcode.Pop: LoadPop(context); break;
  1526. case Opcode.PopMark: LoadPopMark(context); break;
  1527. case Opcode.Proto: LoadProto(context); break;
  1528. case Opcode.Put: LoadPut(context); break;
  1529. case Opcode.Reduce: LoadReduce(context); break;
  1530. case Opcode.SetItem: LoadSetItem(context); break;
  1531. case Opcode.SetItems: LoadSetItems(context); break;
  1532. case Opcode.ShortBinstring: LoadShortBinstring(context); break;
  1533. case Opcode.String: LoadString(context); break;
  1534. case Opcode.Tuple: LoadTuple(context); break;
  1535. case Opcode.Tuple1: LoadTuple1(context); break;
  1536. case Opcode.Tuple2: LoadTuple2(context); break;
  1537. case Opcode.Tuple3: LoadTuple3(context); break;
  1538. case Opcode.Unicode: LoadUnicode(context); break;
  1539. case Opcode.Stop: return PopStack();
  1540. default: throw CannotUnpickle(context, "invalid opcode: {0}", PythonOps.Repr(context, opcode));
  1541. }
  1542. }
  1543. }
  1544. private object PopStack() {
  1545. var res = _stack[_stack.Count - 1];
  1546. _stack.RemoveAt(_stack.Count - 1);
  1547. return res;
  1548. }
  1549. private object PeekStack() {
  1550. return _stack[_stack.Count - 1];
  1551. }
  1552. public object[] StackGetSliceAsArray(int start) {
  1553. object[] res = new object[_stack.Count - start];
  1554. for (int i = 0; i < res.Length; i++) {
  1555. res[i] = _stack[i + start];
  1556. }
  1557. return res;
  1558. }
  1559. [Documentation("noload() -> unpickled object\n\n"
  1560. // 1234567890123456789012345678901234567890123456789012345678901234567890123456789
  1561. + "Like load(), but don't import any modules or create create any instances of\n"
  1562. + "user-defined types. (Builtin objects such as ints, tuples, etc. are created as\n"
  1563. + "with load().)\n"
  1564. + "\n"
  1565. + "This is primarily useful for scanning a pickle for persistent ids without\n"
  1566. + "incurring the overhead of completely unpickling an object. See the pickle\n"
  1567. + "module documentation for more information about persistent ids."
  1568. )]
  1569. public void noload(CodeContext/*!*/ context) {
  1570. throw PythonOps.NotImplementedError("noload() is not implemented");
  1571. }
  1572. private Exception CannotUnpickle(CodeContext/*!*/ context, string format, params object[] args) {
  1573. return PythonExceptions.CreateThrowable(UnpicklingError(context), String.Format(format, args));
  1574. }
  1575. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
  1576. public PythonDictionary memo {
  1577. get {
  1578. if (_memo == null) {
  1579. var newMemo = new PythonDictionary();
  1580. for (int i = 0; i < _privMemo.Count; i++) {
  1581. if (_privMemo[i] != _mark) {
  1582. newMemo[i] = _privMemo[i];
  1583. }
  1584. }
  1585. _memo = newMemo;
  1586. }
  1587. return _memo;
  1588. }
  1589. set {
  1590. _memo = value;
  1591. _privMemo = null;
  1592. }
  1593. }
  1594. public object persistent_load {
  1595. get {
  1596. return _pers_loader;
  1597. }
  1598. set {
  1599. _pers_loader = value;
  1600. }
  1601. }
  1602. private object MemoGet(CodeContext/*!*/ context, int key) {
  1603. object value;
  1604. if (_memo != null) {
  1605. if (_memo.TryGetValue(key, out value)) {
  1606. return value;
  1607. }
  1608. } else if (key < _privMemo.Count && (value = _privMemo[key]) != _mark) {
  1609. return value;
  1610. }
  1611. throw PythonExceptions.CreateThrowable(BadPickleGet(context), String.Format("memo key {0} not found", key));
  1612. }
  1613. private void MemoPut(int key, object value) {
  1614. if (_memo != null) {
  1615. _memo[key] = value;
  1616. } else {
  1617. while (key >= _privMemo.Count) {
  1618. _privMemo.Add(_mark);
  1619. }
  1620. _privMemo[key] = value;
  1621. }
  1622. }
  1623. private int GetMarkIndex(CodeContext/*!*/ context) {
  1624. int i = _stack.Count - 1;
  1625. while (i > 0 && _stack[i] != _mark) i -= 1;
  1626. if (i == -1) throw CannotUnpickle(context, "mark not found");
  1627. return i;
  1628. }
  1629. private string Read(CodeContext/*!*/ context, int size) {
  1630. string res = _file.Read(context, size);
  1631. if (res.Length < size) {
  1632. throw PythonOps.EofError("unexpected EOF while unpickling");
  1633. }
  1634. return res;
  1635. }
  1636. private string ReadLineNoNewline(CodeContext/*!*/ context) {
  1637. string raw = _file.ReadLine(context);
  1638. return raw.Substring(0, raw.Length - 1);
  1639. }
  1640. private object ReadFloatString(CodeContext/*!*/ context) {
  1641. return DoubleOps.__new__(context, TypeCache.Double, ReadLineNoNewline(context));
  1642. }
  1643. private double ReadFloat64(CodeContext/*!*/ context) {
  1644. int index = 0;
  1645. return PythonStruct.CreateDoubleValue(context, ref index, false, Read(context, 8));
  1646. }
  1647. private object ReadIntFromString(CodeContext/*!*/ context) {
  1648. string raw = ReadLineNoNewline(context);
  1649. if ("00" == raw) return ScriptingRuntimeHelpers.False;
  1650. else if ("01" == raw) return ScriptingRuntimeHelpers.True;
  1651. return Int32Ops.__new__(context, TypeCache.Int32, raw);
  1652. }
  1653. private int ReadInt32(CodeContext/*!*/ context) {
  1654. return _file.ReadInt(context);
  1655. }
  1656. private object ReadLongFromString(CodeContext/*!*/ context) {
  1657. return BigIntegerOps.__new__(context, TypeCache.BigInteger, ReadLineNoNewline(context));
  1658. }
  1659. private object ReadLong(CodeContext/*!*/ context, int size) {
  1660. return new BigInteger(Read(context, size).MakeByteArray());
  1661. }
  1662. private char ReadUInt8(CodeContext/*!*/ context) {
  1663. return _file.ReadChar(context);
  1664. }
  1665. private ushort ReadUInt16(CodeContext/*!*/ context) {
  1666. int index = 0;
  1667. return PythonStruct.CreateUShortValue(context, ref index, true, Read(context, 2));
  1668. }
  1669. public object find_global(CodeContext/*!*/ context, object module, object attr) {
  1670. object moduleObject;
  1671. if (!Importer.TryGetExistingModule(context, Converter.ConvertToString(module), out moduleObject)) {
  1672. LightExceptions.CheckAndThrow(Builtin.__import__(context, Converter.ConvertToString(module)));
  1673. moduleObject = context.LanguageContext.SystemStateModules[module];
  1674. }
  1675. return PythonOps.GetBoundAttr(context, moduleObject, Converter.ConvertToString(attr));
  1676. }
  1677. private object MakeInstance(CodeContext/*!*/ context, object cls, object[] args) {
  1678. OldClass oc = cls as OldClass;
  1679. if (oc != null) {
  1680. OldInstance inst = new OldInstance(context, oc);
  1681. if (args.Length != 0 || PythonOps.HasAttr(context, cls, "__getinitargs__")) {
  1682. PythonOps.CallWithContext(context, PythonOps.GetBoundAttr(context, inst, "__init__"), args);
  1683. }
  1684. return inst;
  1685. }
  1686. return PythonOps.CallWithContext(context, cls, args);
  1687. }
  1688. private void PopMark(int markIndex) {
  1689. for (int i = _stack.Count - 1; i >= markIndex; i--) {
  1690. _stack.RemoveAt(i);
  1691. }
  1692. }
  1693. /// <summary>
  1694. /// Interpret everything from markIndex to the top of the stack as a sequence
  1695. /// of key, value, key, value, etc. Set dict[key] = value for each. Pop
  1696. /// everything from markIndex up when done.
  1697. /// </summary>
  1698. private void SetItems(PythonDictionary dict, int markIndex) {
  1699. var storage = dict._storage;
  1700. storage.EnsureCapacityNoLock((_stack.Count - (markIndex + 1)) / 2);
  1701. for (int i = markIndex + 1; i < _stack.Count; i += 2) {
  1702. storage.AddNoLock(ref dict._storage, _stack[i], _stack[i + 1]);
  1703. }
  1704. PopMark(markIndex);
  1705. }
  1706. private void LoadAppend(CodeContext/*!*/ context) {
  1707. object item = PopStack();
  1708. object seq = PeekStack();
  1709. if (seq is List) {
  1710. ((List)seq).append(item);
  1711. } else {
  1712. PythonCalls.Call(context, PythonOps.GetBoundAttr(context, seq, "append"), item);
  1713. }
  1714. }
  1715. private void LoadAppends(CodeContext/*!*/ context) {
  1716. int markIndex = GetMarkIndex(context);
  1717. List seq = (List)_stack[markIndex - 1];
  1718. for (int i = markIndex + 1; i < _stack.Count; i++) {
  1719. seq.AddNoLock(_stack[i]);
  1720. }
  1721. PopMark(markIndex);
  1722. }
  1723. private void LoadBinFloat(CodeContext/*!*/ context) {
  1724. _stack.Add(ReadFloat64(context));
  1725. }
  1726. private void LoadBinGet(CodeContext/*!*/ context) {
  1727. _stack.Add(MemoGet(context, ReadUInt8(context)));
  1728. }
  1729. private void LoadBinInt(CodeContext/*!*/ context) {
  1730. _stack.Add(ReadInt32(context));
  1731. }
  1732. private void LoadBinInt1(CodeContext/*!*/ context) {
  1733. _stack.Add((int)ReadUInt8(context));
  1734. }
  1735. private void LoadBinInt2(CodeContext/*!*/ context) {
  1736. _stack.Add((int)ReadUInt16(context));
  1737. }
  1738. private void LoadBinPersid(CodeContext/*!*/ context) {
  1739. if (_pers_loader == null) throw CannotUnpickle(context, "cannot unpickle binary persistent ID w/o persistent_load");
  1740. _stack.Add(PythonContext.GetContext(context).CallSplat(_pers_loader, PopStack()));
  1741. }
  1742. private void LoadBinPut(CodeContext/*!*/ context) {
  1743. MemoPut(ReadUInt8(context), PeekStack());
  1744. }
  1745. private void LoadBinString(CodeContext/*!*/ context) {
  1746. _stack.Add(Read(context, ReadInt32(context)));
  1747. }
  1748. private void LoadBinUnicode(CodeContext/*!*/ context) {
  1749. string text = Read(context, ReadInt32(context));
  1750. for (int i = 0; i < text.Length; i++) {
  1751. if (text[i] >= 128) {
  1752. _stack.Add(StringOps.decode(context, text, "utf-8", "strict"));
  1753. return;
  1754. }
  1755. }
  1756. _stack.Add(text);
  1757. }
  1758. private void LoadBuild(CodeContext/*!*/ context) {
  1759. object arg = PopStack();
  1760. object inst = PeekStack();
  1761. object setStateCallable;
  1762. if (PythonOps.TryGetBoundAttr(context, inst, "__setstate__", out setStateCallable)) {
  1763. PythonOps.CallWithContext(context, setStateCallable, arg);
  1764. return;
  1765. }
  1766. PythonDictionary dict;
  1767. PythonDictionary slots;
  1768. if (arg == null) {
  1769. dict = null;
  1770. slots = null;
  1771. } else if (arg is PythonDictionary) {
  1772. dict = (PythonDictionary)arg;
  1773. slots = null;
  1774. } else if (arg is PythonTuple) {
  1775. PythonTuple argsTuple = (PythonTuple)arg;
  1776. if (argsTuple.__len__() != 2) {
  1777. throw PythonOps.ValueError("state for object without __setstate__ must be None, dict, or 2-tuple");
  1778. }
  1779. dict = (PythonDictionary)argsTuple[0];
  1780. slots = (PythonDictionary)argsTuple[1];
  1781. } else {
  1782. throw PythonOps.ValueError("state for object without __setstate__ must be None, dict, or 2-tuple");
  1783. }
  1784. if (dict != null) {
  1785. object instDict;
  1786. if (PythonOps.TryGetBoundAttr(context, inst, "__dict__", out instDict)) {
  1787. PythonDictionary realDict = instDict as PythonDictionary;
  1788. if (realDict != null) {
  1789. realDict.update(context, dict);
  1790. } else {
  1791. object updateCallable;
  1792. if (PythonOps.TryGetBoundAttr(context, instDict, "update", out updateCallable)) {
  1793. PythonOps.CallWithContext(context, updateCallable, dict);
  1794. } else {
  1795. throw CannotUnpickle(context, "could not update __dict__ {0} when building {1}", dict, inst);
  1796. }
  1797. }
  1798. }
  1799. }
  1800. if (slots != null) {
  1801. foreach (object key in (IEnumerable)slots) {
  1802. PythonOps.SetAttr(context, inst, (string)key, slots[key]);
  1803. }
  1804. }
  1805. }
  1806. private void LoadDict(CodeContext/*!*/ context) {
  1807. int markIndex = GetMarkIndex(context);
  1808. PythonDictionary dict = new PythonDictionary((_stack.Count - 1 - markIndex) / 2);
  1809. SetItems(dict, markIndex);
  1810. _stack.Add(dict);
  1811. }
  1812. private void LoadDup(CodeContext/*!*/ context) {
  1813. _stack.Add(PeekStack());
  1814. }
  1815. private void LoadEmptyDict(CodeContext/*!*/ context) {
  1816. _stack.Add(new PythonDictionary(new CommonDictionaryStorage()));
  1817. }
  1818. private void LoadEmptyList(CodeContext/*!*/ context) {
  1819. _stack.Add(PythonOps.MakeList());
  1820. }
  1821. private void LoadEmptyTuple(CodeContext/*!*/ context) {
  1822. _stack.Add(PythonTuple.MakeTuple());
  1823. }
  1824. private void LoadExt1(CodeContext/*!*/ context) {
  1825. PythonTuple global = (PythonTuple)PythonCopyReg.GetInvertedRegistry(context)[(int)ReadUInt8(context)];
  1826. _stack.Add(find_global(context, global[0], global[1]));
  1827. }
  1828. private void LoadExt2(CodeContext/*!*/ context) {
  1829. PythonTuple global = (PythonTuple)PythonCopyReg.GetInvertedRegistry(context)[(int)ReadUInt16(context)];
  1830. _stack.Add(find_global(context, global[0], global[1]));
  1831. }
  1832. private void LoadExt4(CodeContext/*!*/ context) {
  1833. PythonTuple global = (PythonTuple)PythonCopyReg.GetInvertedRegistry(context)[ReadInt32(context)];
  1834. _stack.Add(find_global(context, global[0], global[1]));
  1835. }
  1836. private void LoadFloat(CodeContext/*!*/ context) {
  1837. _stack.Add(ReadFloatString(context));
  1838. }
  1839. private void LoadGet(CodeContext/*!*/ context) {
  1840. try {
  1841. _stack.Add(MemoGet(context, (int)ReadIntFromString(context)));
  1842. } catch (ArgumentException) {
  1843. throw PythonExceptions.CreateThrowable(BadPickleGet(context), "while executing GET: invalid integer value");
  1844. }
  1845. }
  1846. private void LoadGlobal(CodeContext/*!*/ context) {
  1847. string module = ReadLineNoNewline(context);
  1848. string attr = ReadLineNoNewline(context);
  1849. _stack.Add(find_global(context, module, attr));
  1850. }
  1851. private void LoadInst(CodeContext/*!*/ context) {
  1852. LoadGlobal(context);
  1853. object cls = PopStack();
  1854. if (cls is OldClass || cls is PythonType) {
  1855. int markIndex = GetMarkIndex(context);
  1856. object[] args = StackGetSliceAsArray(markIndex + 1);
  1857. PopMark(markIndex);
  1858. _stack.Add(MakeInstance(context, cls, args));
  1859. } else {
  1860. throw PythonOps.TypeError("expected class or type after INST, got {0}", DynamicHelpers.GetPythonType(cls));
  1861. }
  1862. }
  1863. private void LoadInt(CodeContext/*!*/ context) {
  1864. _stack.Add(ReadIntFromString(context));
  1865. }
  1866. private void LoadList(CodeContext/*!*/ context) {
  1867. int markIndex = GetMarkIndex(context);
  1868. List list = List.FromArrayNoCopy(StackGetSliceAsArray(markIndex + 1));
  1869. PopMark(markIndex);
  1870. _stack.Add(list);
  1871. }
  1872. private void LoadLong(CodeContext/*!*/ context) {
  1873. _stack.Add(ReadLongFromString(context));
  1874. }
  1875. private void LoadLong1(CodeContext/*!*/ context) {
  1876. int size = ReadUInt8(context);
  1877. if (size == 4) {
  1878. _stack.Add((BigInteger)ReadInt32(context));
  1879. } else {
  1880. _stack.Add(ReadLong(context, size));
  1881. }
  1882. }
  1883. private void LoadLong4(CodeContext/*!*/ context) {
  1884. _stack.Add(ReadLong(context, ReadInt32(context)));
  1885. }
  1886. private void LoadLongBinGet(CodeContext/*!*/ context) {
  1887. _stack.Add(MemoGet(context, (int)ReadInt32(context)));
  1888. }
  1889. private void LoadLongBinPut(CodeContext/*!*/ context) {
  1890. MemoPut(ReadInt32(context), PeekStack());
  1891. }
  1892. private void LoadMark(CodeContext/*!*/ context) {
  1893. _stack.Add(_mark);
  1894. }
  1895. private void LoadNewFalse(CodeContext/*!*/ context) {
  1896. _stack.Add(ScriptingRuntimeHelpers.False);
  1897. }
  1898. private void LoadNewObj(CodeContext/*!*/ context) {
  1899. PythonTuple args = PopStack() as PythonTuple;
  1900. if (args == null) {
  1901. throw PythonOps.TypeError("expected tuple as second argument to NEWOBJ, got {0}", DynamicHelpers.GetPythonType(args));
  1902. }
  1903. PythonType cls = PopStack() as PythonType;
  1904. if (cls == null) {
  1905. throw PythonOps.TypeError("expected new-style type as first argument to NEWOBJ, got {0}", DynamicHelpers.GetPythonType(args));
  1906. }
  1907. PythonTypeSlot dts;
  1908. object value;
  1909. if (cls.TryResolveSlot(context, "__new__", out dts) &&
  1910. dts.TryGetValue(context, null, cls, out value)) {
  1911. object[] newargs = new object[args.__len__() + 1];
  1912. ((ICollection)args).CopyTo(newargs, 1);
  1913. newargs[0] = cls;
  1914. _stack.Add(PythonOps.CallWithContext(context, value, newargs));
  1915. return;
  1916. }
  1917. throw PythonOps.TypeError("didn't find __new__");
  1918. }
  1919. private void LoadNewTrue(CodeContext/*!*/ context) {
  1920. _stack.Add(ScriptingRuntimeHelpers.True);
  1921. }
  1922. private void LoadNoneValue(CodeContext/*!*/ context) {
  1923. _stack.Add(null);
  1924. }
  1925. private void LoadObj(CodeContext/*!*/ context) {
  1926. int markIndex = GetMarkIndex(context);
  1927. if ((markIndex + 1) >= _stack.Count) {
  1928. throw PythonExceptions.CreateThrowable(UnpicklingError(context), "could not find MARK");
  1929. }
  1930. object cls = _stack[markIndex + 1];
  1931. if (cls is OldClass || cls is PythonType) {
  1932. object[] args = StackGetSliceAsArray(markIndex + 2);
  1933. PopMark(markIndex);
  1934. _stack.Add(MakeInstance(context, cls, args));
  1935. } else {
  1936. throw PythonOps.TypeError("expected class or type as first argument to INST, got {0}", DynamicHelpers.GetPythonType(cls));
  1937. }
  1938. }
  1939. private void LoadPersId(CodeContext/*!*/ context) {
  1940. if (_pers_loader == null) {
  1941. throw CannotUnpickle(context, "A load persistent ID instruction is present but no persistent_load function is available");
  1942. }
  1943. _stack.Add(PythonContext.GetContext(context).CallSplat(_pers_loader, ReadLineNoNewline(context)));
  1944. }
  1945. private void LoadPop(CodeContext/*!*/ context) {
  1946. PopStack();
  1947. }
  1948. private void LoadPopMark(CodeContext/*!*/ context) {
  1949. PopMark(GetMarkIndex(context));
  1950. }
  1951. private void LoadProto(CodeContext/*!*/ context) {
  1952. int proto = ReadUInt8(context);
  1953. if (proto > 2) throw PythonOps.ValueError("unsupported pickle protocol: {0}", proto);
  1954. // discard result
  1955. }
  1956. private void LoadPut(CodeContext/*!*/ context) {
  1957. MemoPut((int)ReadIntFromString(context), PeekStack());
  1958. }
  1959. private void LoadReduce(CodeContext/*!*/ context) {
  1960. object args = PopStack();
  1961. object callable = PopStack();
  1962. if (args == null) {
  1963. _stack.Add(PythonCalls.Call(context, PythonOps.GetBoundAttr(context, callable, "__basicnew__")));
  1964. } else if (args.GetType() != typeof(PythonTuple)) {
  1965. throw PythonOps.TypeError(
  1966. "while executing REDUCE, expected tuple at the top of the stack, but got {0}",
  1967. DynamicHelpers.GetPythonType(args)
  1968. );
  1969. }
  1970. _stack.Add(PythonCalls.Call(context, callable, ((PythonTuple)args)._data));
  1971. }
  1972. private void LoadSetItem(CodeContext/*!*/ context) {
  1973. object value = PopStack();
  1974. object key = PopStack();
  1975. PythonDictionary dict = PeekStack() as PythonDictionary;
  1976. if (dict == null) {
  1977. throw PythonOps.TypeError(
  1978. "while executing SETITEM, expected dict at stack[-3], but got {0}",
  1979. DynamicHelpers.GetPythonType(PeekStack())
  1980. );
  1981. }
  1982. dict[key] = value;
  1983. }
  1984. private void LoadSetItems(CodeContext/*!*/ context) {
  1985. int markIndex = GetMarkIndex(context);
  1986. PythonDictionary dict = _stack[markIndex - 1] as PythonDictionary;
  1987. if (dict == null) {
  1988. throw PythonOps.TypeError(
  1989. "while executing SETITEMS, expected dict below last mark, but got {0}",
  1990. DynamicHelpers.GetPythonType(_stack[markIndex - 1])
  1991. );
  1992. }
  1993. SetItems(dict, markIndex);
  1994. }
  1995. private void LoadShortBinstring(CodeContext/*!*/ context) {
  1996. _stack.Add(Read(context, ReadUInt8(context)));
  1997. }
  1998. private void LoadString(CodeContext/*!*/ context) {
  1999. string repr = ReadLineNoNewline(context);
  2000. if (repr.Length < 2 ||
  2001. !(
  2002. repr[0] == '"' && repr[repr.Length - 1] == '"' ||
  2003. repr[0] == '\'' && repr[repr.Length - 1] == '\''
  2004. )
  2005. ) {
  2006. throw PythonOps.ValueError("while executing STRING, expected string that starts and ends with quotes");
  2007. }
  2008. _stack.Add(StringOps.decode(context, repr.Substring(1, repr.Length - 2), "string-escape", "strict"));
  2009. }
  2010. private void LoadTuple(CodeContext/*!*/ context) {
  2011. int markIndex = GetMarkIndex(context);
  2012. PythonTuple tuple = PythonTuple.MakeTuple(StackGetSliceAsArray(markIndex + 1));
  2013. PopMark(markIndex);
  2014. _stack.Add(tuple);
  2015. }
  2016. private void LoadTuple1(CodeContext/*!*/ context) {
  2017. object item0 = PopStack();
  2018. _stack.Add(PythonTuple.MakeTuple(item0));
  2019. }
  2020. private void LoadTuple2(CodeContext/*!*/ context) {
  2021. object item1 = PopStack();
  2022. object item0 = PopStack();
  2023. _stack.Add(PythonTuple.MakeTuple(item0, item1));
  2024. }
  2025. private void LoadTuple3(CodeContext/*!*/ context) {
  2026. object item2 = PopStack();
  2027. object item1 = PopStack();
  2028. object item0 = PopStack();
  2029. _stack.Add(PythonTuple.MakeTuple(item0, item1, item2));
  2030. }
  2031. private void LoadUnicode(CodeContext/*!*/ context) {
  2032. _stack.Add(StringOps.decode(context, ReadLineNoNewline(context), "raw-unicode-escape", "strict"));
  2033. }
  2034. }
  2035. #endregion
  2036. private static PythonType PicklingError(CodeContext/*!*/ context) {
  2037. return (PythonType)PythonContext.GetContext(context).GetModuleState("PicklingError");
  2038. }
  2039. private static PythonType PickleError(CodeContext/*!*/ context) {
  2040. return (PythonType)PythonContext.GetContext(context).GetModuleState("PickleError");
  2041. }
  2042. private static PythonType UnpicklingError(CodeContext/*!*/ context) {
  2043. return (PythonType)PythonContext.GetContext(context).GetModuleState("UnpicklingError");
  2044. }
  2045. private static PythonType BadPickleGet(CodeContext/*!*/ context) {
  2046. return (PythonType)PythonContext.GetContext(context).GetModuleState("BadPickleGet");
  2047. }
  2048. class ReferenceEqualityComparer : IEqualityComparer<object> {
  2049. public static ReferenceEqualityComparer Instance = new ReferenceEqualityComparer();
  2050. #region IEqualityComparer<object> Members
  2051. public new bool Equals(object x, object y) {
  2052. return x == y;
  2053. }
  2054. public int GetHashCode(object obj) {
  2055. return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(obj);
  2056. }
  2057. #endregion
  2058. }
  2059. }
  2060. }