Exemplo n.º 1
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));
        }
Exemplo n.º 2
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);
                }
            }
        }