コード例 #1
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);
        }
コード例 #2
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);
                }
            }
        }