Beispiel #1
0
 private void ProcessBuffer(PyBuffer buffer)
 {
     this.mStringBuilder.AppendFormat(
         "[PyBuffer {0} bytes: {1}]", buffer.Length, HexDump.ByteArrayToHexViaLookup32(buffer.Value)
         );
     this.mStringBuilder.AppendLine();
 }
Beispiel #2
0
        /* This should never be used in the node, just the Cache Tool */
        public bool Decode(PyObject from)
        {
            PyTuple data = (from as PyObjectData).Arguments as PyTuple;

            // Just decode the cache info for now..
            cache = data.Items[4] as PyBuffer;

            return(true);
        }
Beispiel #3
0
        public void TestBufferRead()
        {
            string bufferTestString = "hello world! !$%&/()=?";

            using var _ = Py.GIL();

            using var pythonArray = ByteArrayFromAsciiString(bufferTestString);
            byte[] managedArray = new byte[bufferTestString.Length];

            using (PyBuffer buf = pythonArray.GetBuffer())
            {
                managedArray[0] = (byte)' ';
                buf.Read(managedArray, 1, managedArray.Length - 1, 1);
            }

            string result = new UTF8Encoding().GetString(managedArray);

            Assert.IsTrue(result == " " + bufferTestString.Substring(1));
        }
Beispiel #4
0
        public void TestBufferWrite()
        {
            string bufferTestString  = "hello world! !$%&/()=?";
            string bufferTestString2 = "h llo world! !$%&/()=?";

            using var _ = Py.GIL();

            using var pythonArray = ByteArrayFromAsciiString(bufferTestString);

            using (PyBuffer buf = pythonArray.GetBuffer(PyBUF.WRITABLE))
            {
                byte[] managedArray = { (byte)' ' };
                buf.Write(managedArray, 0, managedArray.Length, 1);
            }

            string result = pythonArray.InvokeMethod("decode", "utf-8".ToPython()).As <string>();

            Assert.IsTrue(result == bufferTestString2);
        }
Beispiel #5
0
        public void TestBufferRead()
        {
            string bufferTestString = "hello world! !$%&/()=?";

            using (Py.GIL())
            {
                using (var scope = Py.CreateScope())
                {
                    scope.Exec($"arr = b'{bufferTestString}'");
                    PyObject pythonArray  = scope.Get("arr");
                    byte[]   managedArray = new byte[bufferTestString.Length];

                    using (PyBuffer buf = pythonArray.GetBuffer())
                    {
                        buf.Read(managedArray, 0, managedArray.Length);
                    }

                    string result = new UTF8Encoding().GetString(managedArray);
                    Assert.IsTrue(result == bufferTestString);
                }
            }
        }
Beispiel #6
0
        public void TestBufferWrite()
        {
            string bufferTestString = "hello world! !$%&/()=?";

            using (Py.GIL())
            {
                using (var scope = Py.CreateScope())
                {
                    scope.Exec($"arr = bytearray({bufferTestString.Length})");
                    PyObject pythonArray  = scope.Get("arr");
                    byte[]   managedArray = new UTF8Encoding().GetBytes(bufferTestString);

                    using (PyBuffer buf = pythonArray.GetBuffer())
                    {
                        buf.Write(managedArray, 0, managedArray.Length);
                    }

                    string result = scope.Eval("arr.decode('utf-8')").ToString();
                    Assert.IsTrue(result == bufferTestString);
                }
            }
        }
Beispiel #7
0
 /// <summary>
 /// Converts the given <paramref name="data"/> to it's byte array representation.
 /// Buffer are basic, binary byte arrays that do not follow any specific format
 /// Their length is variable and is indicated by an extended size indicator
 ///
 /// The following opcodes are supported
 /// <seealso cref="Opcode.Buffer" /> 2 bytes minimum, the data comes right after the length indicator
 /// </summary>
 /// <param name="writer">Where to write the encoded data to</param>
 /// <param name="buffer">The value to write</param>
 private static void ProcessBuffer(BinaryWriter writer, PyBuffer buffer)
 {
     writer.WriteOpcode(Opcode.Buffer);
     writer.WriteSizeEx(buffer.Value.Length);
     writer.Write(buffer);
 }