Пример #1
0
        public void pickle(object o, Stream outs, Pickler currentPickler)
        {
            PyroProxy proxy = (PyroProxy)o;

            outs.WriteByte(Opcodes.GLOBAL);
            var output = Encoding.Default.GetBytes("Pyro4.core\nProxy\n");

            outs.Write(output, 0, output.Length);
            outs.WriteByte(Opcodes.EMPTY_TUPLE);
            outs.WriteByte(Opcodes.NEWOBJ);

            // args(8): pyroUri, pyroOneway(hashset), pyroMethods(set), pyroAttrs(set), pyroTimeout, pyroHmacKey, pyroHandshake, pyroMaxRetries
            object[] args =
            {
                new PyroURI(proxy.objectid, proxy.hostname, proxy.port),
                proxy.pyroOneway,
                proxy.pyroMethods,
                proxy.pyroAttrs,
                0.0,
                proxy.pyroHmacKey,
                proxy.pyroHandshake,
                0          // maxretries is not yet supported/used by pyrolite
            };
            currentPickler.save(args);
            outs.WriteByte(Opcodes.BUILD);
        }
Пример #2
0
        public void testMemoizationString()
        {
            string str = "a";

            object[] array = new object[] { str, str };

            Pickler p = new Pickler(true);

            byte[] data = p.dumps(array);
            Assert.Contains(Opcodes.BINPUT, data);     // check that memoization was done

            Unpickler u = new Unpickler();

            object[] result = (object[])u.loads(data);
            Assert.AreEqual(2, result.Length);
            object first  = result[0];
            object second = result[1];

            Assert.IsInstanceOf(typeof(string), first);
            Assert.IsInstanceOf(typeof(string), second);
            Assert.AreSame(first, second);                              // both objects should be the same memoized object

            str = (string)second;
            Assert.AreEqual("a", str);
        }
Пример #3
0
        public void testMappings()
        {
            byte[]    o;
            Pickler   p      = new Pickler(false);
            Unpickler pu     = new Unpickler();
            var       intmap = new Dictionary <int, int>();

            intmap.Add(1, 11);
            intmap.Add(2, 22);
            intmap.Add(3, 33);
            o = p.dumps(intmap);
            Hashtable resultmap = (Hashtable)pu.loads(o);

            AssertUtils.AssertEqual(intmap, resultmap);

            var stringmap = new Dictionary <string, string>();

            stringmap.Add("A", "1");
            stringmap.Add("B", "2");
            stringmap.Add("C", "3");
            o         = p.dumps(stringmap);
            resultmap = (Hashtable)pu.loads(o);
            AssertUtils.AssertEqual(stringmap, resultmap);

            Hashtable table = new Hashtable();

            table.Add(1, 11);
            table.Add(2, 22);
            table.Add(3, 33);
            o         = p.dumps(table);
            resultmap = (Hashtable)pu.loads(o);
            AssertUtils.AssertEqual(table, resultmap);
        }
Пример #4
0
        public void testSets()
        {
            byte[]    o;
            Pickler   p  = new Pickler(false);
            Unpickler up = new Unpickler();

            var intset = new HashSet <int>();

            intset.Add(1);
            intset.Add(2);
            intset.Add(3);
            o = p.dumps(intset);
            HashSet <object> resultset = (HashSet <object>)up.loads(o);

            AssertUtils.AssertEqual(intset, resultset);

            HashSet <string> stringset = new HashSet <string>();

            stringset.Add("A");
            stringset.Add("B");
            stringset.Add("C");
            o         = p.dumps(stringset);
            resultset = (HashSet <object>)up.loads(o);
            AssertUtils.AssertEqual(stringset, resultset);
        }
Пример #5
0
        public void TestMemoizationMap()
        {
            var map = new Dictionary <string, string> {
                { "key", "value" }
            };

            object[] array = { map, map };

            Pickler p = new Pickler(true);

            byte[] data = p.dumps(array);
            Assert.Contains(Opcodes.BINPUT, data);     // check that memoization was done

            Unpickler u = new Unpickler();

            object[] result = (object[])u.loads(data);
            Assert.Equal(2, result.Length);
            object first  = result[0];
            object second = result[1];

            Assert.IsType <Hashtable>(first);
            Assert.IsType <Hashtable>(second);
            Assert.Same(first, second);                                 // both objects should be the same memoized object

            Hashtable theMap = (Hashtable)second;

            Assert.Single(theMap);
            Assert.Equal("value", theMap["key"]);
        }
Пример #6
0
        public void TestMemoizationSet()
        {
            var set = new HashSet <string> {
                "a"
            };

            object[] array = { set, set };

            Pickler p = new Pickler(true);

            byte[] data = p.dumps(array);
            Assert.Contains(Opcodes.BINPUT, data);     // check that memoization was done

            Unpickler u = new Unpickler();

            object[] result = (object[])u.loads(data);
            Assert.Equal(2, result.Length);
            object first  = result[0];
            object second = result[1];

            Assert.IsType <HashSet <object> >(first);
            Assert.IsType <HashSet <object> >(second);
            Assert.Same(first, second);                                 // both objects should be the same memoized object

            HashSet <object> theSet = (HashSet <object>)second;

            Assert.Single(theSet);
            Assert.Contains("a", theSet);
        }
Пример #7
0
        public void TestMappings()
        {
            Pickler   p      = new Pickler(false);
            Unpickler pu     = new Unpickler();
            var       intmap = new Dictionary <int, int> {
                { 1, 11 }, { 2, 22 }, { 3, 33 }
            };
            var       o         = p.dumps(intmap);
            Hashtable resultmap = (Hashtable)pu.loads(o);

            AssertUtils.AssertEqual(intmap, resultmap);

            var stringmap = new Dictionary <string, string> {
                { "A", "1" }, { "B", "2" }, { "C", "3" }
            };

            o         = p.dumps(stringmap);
            resultmap = (Hashtable)pu.loads(o);
            AssertUtils.AssertEqual(stringmap, resultmap);

            Hashtable table = new Hashtable {
                { 1, 11 }, { 2, 22 }, { 3, 33 }
            };

            o         = p.dumps(table);
            resultmap = (Hashtable)pu.loads(o);
            AssertUtils.AssertEqual(table, resultmap);
        }
Пример #8
0
        public void TestInterfaceHierarchyPickler()
        {
            BaseClassWithInterface b   = new BaseClassWithInterface();
            SubClassWithInterface  sub = new SubClassWithInterface();
            Pickler p = new Pickler(false);

            try {
                p.dumps(b);
                Assert.True(false, "should crash");
            } catch (PickleException x)
            {
                Assert.Contains("couldn't pickle object of type", x.Message);
            }
            try {
                p.dumps(sub);
                Assert.True(false, "should crash");
            } catch (PickleException x) {
                Assert.Contains("couldn't pickle object of type", x.Message);
            }
            Pickler.registerCustomPickler(typeof(IBaseInterface), new AnyClassPickler());
            byte[] data = p.dumps(b);
            Assert.Contains("[class=PickleTests.PicklerTests+BaseClassWithInterface]", S(data));
            data = p.dumps(sub);
            Assert.Contains("[class=PickleTests.PicklerTests+SubClassWithInterface]", S(data));
        }
Пример #9
0
        public void testArrays()
        {
            Pickler p = new Pickler(false);

            byte[] o;
            o = p.dumps(new string[] {});
            Assert.AreEqual(B(")"), o);
            o = p.dumps(new string[] { "abc" });
            Assert.AreEqual(B("X\u0003\u0000\u0000\u0000abc\u0085"), o);
            o = p.dumps(new string[] { "abc", "def" });
            Assert.AreEqual(B("X\u0003\u0000\u0000\u0000abcX\u0003\u0000\u0000\u0000def\u0086"), o);
            o = p.dumps(new string[] { "abc", "def", "ghi" });
            Assert.AreEqual(B("X\u0003\u0000\u0000\u0000abcX\u0003\u0000\u0000\u0000defX\u0003\u0000\u0000\u0000ghi\u0087"), o);
            o = p.dumps(new string[] { "abc", "def", "ghi", "jkl" });
            Assert.AreEqual(B("(X\u0003\u0000\u0000\u0000abcX\u0003\u0000\u0000\u0000defX\u0003\u0000\u0000\u0000ghiX\u0003\u0000\u0000\u0000jklt"), o);

            o = p.dumps(new char[] { 'A', 'B', 'C' });
            Assert.AreEqual(B("X\u0003\u0000\u0000\u0000ABC"), o);

            o = p.dumps(new bool[] { true, false, true });
            Assert.AreEqual(B("\u0088\u0089\u0088\u0087"), o);

            o = p.dumps(new byte[] { 1, 2, 3 });
            Assert.AreEqual(B("c__builtin__\nbytearray\nX\u0003\u0000\u0000\u0000\u0001\u0002\u0003X\u0007\u0000\u0000\u0000latin-1\u0086R"), o);

            o = p.dumps(new int[] { 1, 2, 3 });
            Assert.AreEqual(B("carray\narray\nU\u0001i](K\u0001K\u0002K\u0003e\u0086R"), o);

            o = p.dumps(new double[] { 1.1, 2.2, 3.3 });
            Assert.AreEqual(B("carray\narray\nU\u0001d](G?\u00f1\u0099\u0099\u0099\u0099\u0099\u009aG@\u0001\u0099\u0099\u0099\u0099\u0099\u009aG@\nffffffe\u0086R"), o);
        }
Пример #10
0
        public void testMemoizationSet()
        {
            var set = new HashSet <string>();

            set.Add("a");
            object[] array = new object[] { set, set };

            Pickler p = new Pickler(true);

            byte[] data = p.dumps(array);
            Assert.Contains(Opcodes.BINPUT, data);     // check that memoization was done

            Unpickler u = new Unpickler();

            object[] result = (object[])u.loads(data);
            Assert.AreEqual(2, result.Length);
            object first  = result[0];
            object second = result[1];

            Assert.IsInstanceOf(typeof(HashSet <object>), first);
            Assert.IsInstanceOf(typeof(HashSet <object>), second);
            Assert.AreSame(first, second);                              // both objects should be the same memoized object

            HashSet <object> theSet = (HashSet <object>)second;

            Assert.AreEqual(1, theSet.Count);
            Assert.IsTrue(theSet.Contains("a"));
        }
Пример #11
0
        public void testMemoizationTimeStuff()
        {
            TimeSpan delta = new TimeSpan(1, 2, 3);
            DateTime time  = new DateTime(2014, 11, 20, 1, 2, 3);

            object[] array = new object[] { delta, delta, time, time };

            Pickler p = new Pickler(true);

            byte[] data = p.dumps(array);
            Assert.Contains(Opcodes.BINPUT, data);     // check that memoization was done

            Unpickler u = new Unpickler();

            object[] result = (object[])u.loads(data);
            Assert.AreEqual(4, result.Length);
            Assert.IsInstanceOf(typeof(TimeSpan), result[0]);
            Assert.IsInstanceOf(typeof(TimeSpan), result[1]);
            Assert.IsInstanceOf(typeof(DateTime), result[2]);
            Assert.IsInstanceOf(typeof(DateTime), result[3]);
            Assert.AreSame(result[0], result[1]);                               // both objects should be the same memoized object
            Assert.AreSame(result[2], result[3]);                               // both objects should be the same memoized object

            delta = (TimeSpan)result[1];
            time  = (DateTime)result[3];
            Assert.AreEqual(new TimeSpan(1, 2, 3), delta);
            Assert.AreEqual(new DateTime(2014, 11, 20, 1, 2, 3), time);
        }
Пример #12
0
        public void testMemoizationMap()
        {
            var map = new Dictionary <string, string>();

            map.Add("key", "value");
            object[] array = new object[] { map, map };

            Pickler p = new Pickler(true);

            byte[] data = p.dumps(array);
            Assert.Contains(Opcodes.BINPUT, data);     // check that memoization was done

            Unpickler u = new Unpickler();

            object[] result = (object[])u.loads(data);
            Assert.AreEqual(2, result.Length);
            object first  = result[0];
            object second = result[1];

            Assert.IsInstanceOf(typeof(Hashtable), first);
            Assert.IsInstanceOf(typeof(Hashtable), second);
            Assert.AreSame(first, second);                              // both objects should be the same memoized object

            Hashtable theMap = (Hashtable)second;

            Assert.AreEqual(1, theMap.Count);
            Assert.AreEqual("value", theMap["key"]);
        }
Пример #13
0
        public void testMemoizationCollection()
        {
            ICollection <string> list = new List <string>();

            list.Add("a");
            object[] array = new object[] { list, list };

            Pickler p = new Pickler(true);

            byte[] data = p.dumps(array);
            Assert.Contains(Opcodes.BINPUT, data);     // check that memoization was done

            Unpickler u = new Unpickler();

            object[] result = (object[])u.loads(data);
            Assert.AreEqual(2, result.Length);
            object first  = result[0];
            object second = result[1];

            Assert.IsInstanceOf(typeof(ArrayList), first);
            Assert.IsInstanceOf(typeof(ArrayList), second);
            Assert.AreSame(first, second);                              // both objects should be the same memoized object

            ArrayList theList = (ArrayList)second;

            Assert.AreEqual(1, theList.Count);
            Assert.IsTrue(theList.Contains("a"));
        }
Пример #14
0
        public void pickle(object o, Stream stream, Pickler currentPickler)
        {
            if (o.Equals(this))
            {
                SerDe.Write(stream, Opcodes.GLOBAL);
                SerDe.Write(stream, Encoding.UTF8.GetBytes(
                                $"{_module}\n_create_row_inbound_converter\n"));
            }
            else
            {
                if (!(o is Row row))
                {
                    throw new InvalidOperationException("A Row object is expected.");
                }

                currentPickler.save(this);
                currentPickler.save(row.Schema);
                SerDe.Write(stream, Opcodes.TUPLE1);
                SerDe.Write(stream, Opcodes.REDUCE);

                SerDe.Write(stream, Opcodes.MARK);
                for (int i = 0; i < row.Size(); ++i)
                {
                    currentPickler.save(row.Get(i));
                }

                SerDe.Write(stream, Opcodes.TUPLE);
                SerDe.Write(stream, Opcodes.REDUCE);
            }
        }
Пример #15
0
	public void testPickleUnpickleProxy() {
		PyroProxy proxy=new PyroProxy("hostname",9999,"objectid");
		Pickler p=new Pickler();
		byte[] pickled_proxy=p.dumps(proxy);
		object result=U(pickled_proxy);
		Assert.IsInstanceOfType(typeof(System.Collections.Hashtable), result); // proxy objects cannot be properly pickled and are pickled as bean, hence HashMap
	}
Пример #16
0
 public override byte[] serializeData(object obj)
 {
     using (var p = new Pickler())
     {
         return(p.dumps(obj));
     }
 }
Пример #17
0
        public void pickle(object o, Stream stream, Pickler currentPickler)
        {
            if (o.Equals(this))
            {
                SerDe.Write(stream, Opcodes.GLOBAL);
                SerDe.Write(stream, Encoding.UTF8.GetBytes(module + "\n" + "_create_row_inbound_converter" + "\n"));
            }
            else
            {
                var row = o as Row;
                if (row == null)
                {
                    throw new InvalidOperationException(this.GetType().Name + " only accepts 'Row' type objects.");
                }

                currentPickler.save(this);
                currentPickler.save(row.GetSchema());
                SerDe.Write(stream, Opcodes.TUPLE1);
                SerDe.Write(stream, Opcodes.REDUCE);

                SerDe.Write(stream, Opcodes.MARK);

                var i = 0;
                while (i < row.Size())
                {
                    currentPickler.save(row.Get(i));
                    i++;
                }

                SerDe.Write(stream, Opcodes.TUPLE);
                SerDe.Write(stream, Opcodes.REDUCE);
            }
        }
Пример #18
0
        public void TestRecursiveArray2()
        {
            Pickler p = new Pickler(false);

            object[] a = { "hello", "placeholder" };
            a[1] = a;                                          // make it recursive
            Assert.Throws <PickleException>(() => p.dumps(a)); // "recursive array not supported, use list"
        }
Пример #19
0
 public static string dumps(CodeContext/*!*/ context, object obj, [DefaultParameterValue(null)] object protocol, [DefaultParameterValue(null)] object bin) {
     //??? possible perf enhancement: use a C# TextWriter-backed IFileOutput and
     // thus avoid Python call overhead. Also do similar thing for LoadFromString.
     object stringIO = PythonOps.Invoke(context, DynamicHelpers.GetPythonTypeFromType(typeof(PythonStringIO)), SymbolTable.StringToId("StringIO"));
     Pickler pickler = new Pickler(context, stringIO, protocol, bin);
     pickler.dump(context, obj);
     return Converter.ConvertToString(PythonOps.Invoke(context, stringIO, SymbolTable.StringToId("getvalue")));
 }
Пример #20
0
        public void TestRecursiveArray6()
        {
            Pickler p = new Pickler(false);

            object[] a = { "a", "b", "c", "d", "e", "f" };
            a[5] = a;                                          // make it recursive
            Assert.Throws <PickleException>(() => p.dumps(a)); // "recursive array not supported, use list"
        }
Пример #21
0
 public override byte[] serializeCall(string objectId, string method, object[] vargs, IDictionary <string, object> kwargs)
 {
     using (var p = new Pickler())
     {
         object[] invokeparams = new object[] { objectId, method, vargs, kwargs };
         return(p.dumps(invokeparams));
     }
 }
Пример #22
0
        public void testCustomPickler()
        {
            Pickler.registerCustomPickler(typeof(CustomClass), new CustomClassPickler());
            CustomClass c = new CustomClass();
            Pickler     p = new Pickler(false);

            p.dumps(c);
        }
Пример #23
0
        public void TestRecursiveArray6()
        {
            Pickler p = new Pickler(false);

            object[] a = new object[] { "a", "b", "c", "d", "e", "f" };
            a[5] = a;     // make it recursive
            p.dumps(a);
        }
Пример #24
0
        public void TestRecursiveArray2()
        {
            Pickler p = new Pickler(false);

            object[] a = new object[] { "hello", "placeholder" };
            a[1] = a;     // make it recursive
            p.dumps(a);
        }
Пример #25
0
        public void testFailure()
        {
            NotABean notabean = new NotABean();

            notabean.x = 42;
            Pickler p = new Pickler(false);

            p.dumps(notabean);
        }
Пример #26
0
        public void TestFailure()
        {
            NotABean notabean = new NotABean {
                Xantippe = 42
            };
            Pickler p = new Pickler(false);

            Assert.Throws <PickleException>(() => p.dumps(notabean));
        }
Пример #27
0
        public void TestZeroToTwoFiveSix()
        {
            var bytes = new byte[256];

            for (int b = 0; b < 256; ++b)
            {
                bytes[b] = (byte)b;
            }
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < 256; ++i)
            {
                sb.Append((char)i);
            }
            string str = sb.ToString();

            Pickler   p = new Pickler(false);
            Unpickler u = new Unpickler();

            MemoryStream bos = new MemoryStream(434);

            bos.WriteByte(Opcodes.PROTO); bos.WriteByte(2);
            var data = Encoding.Default.GetBytes("c__builtin__\nbytearray\n");

            bos.Write(data, 0, data.Length);
            bos.WriteByte(Opcodes.BINUNICODE);
            bos.Write(new byte[] { 0x80, 0x01, 0x00, 0x00 }, 0, 4);
            var utf8 = Encoding.UTF8.GetBytes(str);

            bos.Write(utf8, 0, utf8.Length);
            bos.WriteByte(Opcodes.BINUNICODE);
            bos.Write(new byte[] { 7, 0, 0, 0 }, 0, 4);
            data = Encoding.Default.GetBytes("latin-1");
            bos.Write(data, 0, data.Length);
            bos.WriteByte(Opcodes.TUPLE2);
            bos.WriteByte(Opcodes.REDUCE);
            bos.WriteByte(Opcodes.STOP);

            var bytesresult = bos.ToArray();
            var output      = p.dumps(bytes);

            Assert.Equal(bytesresult, output);
            Assert.Equal(bytes, (byte[])u.loads(output));

            bos = new MemoryStream(434);
            bos.WriteByte(Opcodes.PROTO); bos.WriteByte(2);
            bos.WriteByte(Opcodes.BINUNICODE);
            bos.Write(new byte[] { 0x80, 0x01, 0x00, 0x00 }, 0, 4);
            utf8 = Encoding.UTF8.GetBytes(str);
            bos.Write(utf8, 0, utf8.Length);
            bos.WriteByte(Opcodes.STOP);
            bytesresult = bos.ToArray();

            output = p.dumps(str);
            Assert.Equal(bytesresult, output);
            Assert.Equal(str, u.loads(output));
        }
Пример #28
0
        public void TestMemoizationRecursiveNoMemo()
        {
            Pickler p = new Pickler(false);

            const string reused = "reused";
            IList        list   = new ArrayList();

            list.Add(reused);
            list.Add(reused);
            list.Add(list);
            Assert.Throws <StackOverflowException>(() => p.dumps(list));
        }
Пример #29
0
        public void testMemoizationRecursiveNoMemo()
        {
            Pickler p = new Pickler(false);

            string reused = "reused";
            IList  list   = new ArrayList();

            list.Add(reused);
            list.Add(reused);
            list.Add(list);
            p.dumps(list);
        }
Пример #30
0
	public void testPickleUnpickleURI() {
		PyroURI uri=new PyroURI("PYRO:test@localhost:9999");
		Pickler p=new Pickler();
		byte[] pickled_uri=p.dumps(uri);
		PyroURI uri2=(PyroURI) U(pickled_uri);
		Assert.AreEqual(uri,uri2);

		uri=new PyroURI();
		pickled_uri=p.dumps(uri);
		uri2=(PyroURI) U(pickled_uri);
		Assert.AreEqual(uri,uri2);
	}
Пример #31
0
        public void testCustomPickler()
        {
            Pickler.registerCustomPickler(typeof(CustomClass), new CustomClassPickler());
            CustomClass c = new CustomClass();
            Pickler     p = new Pickler(false);

            byte[] ser = p.dumps(c);

            Unpickler u = new Unpickler();
            string    x = (string)u.loads(ser);

            Assert.AreEqual("customclassint=42", x);
        }
Пример #32
0
 public static void DumpToFile(ICallerContext context, object obj, object file, [DefaultParameterValue(null)] object protocol, [DefaultParameterValue(null)] object bin)
 {
     Pickler pickler = new Pickler(file, protocol, bin);
     pickler.Dump(context, obj);
 }
Пример #33
0
 public static void dump(CodeContext/*!*/ context, object obj, object file, [DefaultParameterValue(null)] object protocol, [DefaultParameterValue(null)] object bin) {
     Pickler pickler = new Pickler(context, file, protocol, bin);
     pickler.dump(context, obj);
 }