-
Notifications
You must be signed in to change notification settings - Fork 774
Expand file tree
/
Copy pathTestPyBuffer.cs
More file actions
160 lines (131 loc) · 4.75 KB
/
TestPyBuffer.cs
File metadata and controls
160 lines (131 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Runtime.CompilerServices;
using System.Text;
using NUnit.Framework;
using Python.Runtime;
using Python.Runtime.Codecs;
namespace Python.EmbeddingTest {
class TestPyBuffer
{
[OneTimeSetUp]
public void SetUp()
{
TupleCodec<ValueTuple>.Register();
}
[OneTimeTearDown]
public void Dispose()
{
PyObjectConversions.Reset();
}
[Test]
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.That(result == bufferTestString2, Is.True);
}
[Test]
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.That(result == " " + bufferTestString.Substring(1), Is.True);
}
[Test]
public void ArrayHasBuffer()
{
var array = new[,] {{1, 2}, {3,4}};
var memoryView = PythonEngine.Eval("memoryview");
var mem = memoryView.Invoke(array.ToPython());
Assert.That(mem[(0, 0).ToPython()].As<int>(), Is.EqualTo(1));
Assert.That(mem[(1, 0).ToPython()].As<int>(), Is.EqualTo(array[1, 0]));
}
[Test]
public void RefCount()
{
using var _ = Py.GIL();
using var arr = ByteArrayFromAsciiString("hello world! !$%&/()=?");
Assert.That(arr.Refcount, Is.EqualTo(1));
using (PyBuffer buf = arr.GetBuffer())
{
Assert.That(arr.Refcount, Is.EqualTo(2));
}
Assert.That(arr.Refcount, Is.EqualTo(1));
}
[Test]
public void Finalization()
{
if (Type.GetType("Mono.Runtime") is not null)
{
Assert.Inconclusive("test unreliable in Mono");
return;
}
using var _ = Py.GIL();
using var arr = ByteArrayFromAsciiString("hello world! !$%&/()=?");
Assert.That(arr.Refcount, Is.EqualTo(1));
MakeBufAndLeak(arr);
GC.Collect();
GC.WaitForPendingFinalizers();
Finalizer.Instance.Collect();
Assert.That(arr.Refcount, Is.EqualTo(1));
}
[Test]
public void MultidimensionalNumPyArray()
{
var ndarray = np.arange(24).reshape(1,2,3,4).T;
PyObject ndim = ndarray.ndim;
PyObject shape = ndarray.shape;
PyObject strides = ndarray.strides;
PyObject contiguous = ndarray.flags["C_CONTIGUOUS"];
using PyBuffer buf = ndarray.GetBuffer(PyBUF.STRIDED);
Assert.Multiple(() =>
{
Assert.That(buf.Dimensions, Is.EqualTo(ndim.As<int>()));
Assert.That(buf.Shape, Is.EqualTo(shape.As<long[]>()));
Assert.That(buf.Strides, Is.EqualTo(strides.As<long[]>()));
Assert.That(buf.IsContiguous(BufferOrderStyle.C), Is.EqualTo(contiguous.As<bool>()));
});
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void MakeBufAndLeak(PyObject bufProvider)
{
PyBuffer buf = bufProvider.GetBuffer();
}
static PyObject ByteArrayFromAsciiString(string str)
{
using var scope = Py.CreateScope();
return Runtime.Runtime.PyByteArray_FromStringAndSize(str).MoveToPyObject();
}
dynamic np
{
get
{
try
{
return Py.Import("numpy");
}
catch (PythonException)
{
Assert.Inconclusive("Numpy or dependency not installed");
return null;
}
}
}
}
}