| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,11 +2,14 @@ | |||
| 2 | 2 | using System.Runtime.CompilerServices; | |
| 3 | 3 | using System.Text; | |
| 4 | 4 | using System.Threading; | |
| 5 | + | ||
| 5 | 6 | using NUnit.Framework; | |
| 7 | + | ||
| 6 | 8 | using Python.Runtime; | |
| 7 | 9 | using Python.Runtime.Codecs; | |
| 8 | 10 | ||
| 9 | - namespace Python.EmbeddingTest { | ||
| 11 | + namespace Python.EmbeddingTest | ||
| 12 | + { | ||
| 10 | 13 | class TestPyBuffer | |
| 11 | 14 | { | |
| 12 | 15 | [OneTimeSetUp] | |
@@ -70,16 +73,37 @@ public void GetPointer() | |||
| 70 | 73 | ||
| 71 | 74 | using PyBuffer buf = ndArray.GetBuffer(PyBUF.STRIDES); | |
| 72 | 75 | ||
| 73 | - // this throws System.ExecutionEngineException | ||
| 74 | 76 | var ptr = buf.GetPointer([0, 0, 0]); | |
| 75 | 77 | ||
| 76 | 78 | Assert.That(ptr, Is.Not.EqualTo(IntPtr.Zero)); | |
| 79 | + | ||
| 80 | + var ptrEnd = buf.GetPointer([9, 9, 9]); | ||
| 81 | + Assert.That(ptrEnd, Is.Not.EqualTo(IntPtr.Zero)); | ||
| 82 | + | ||
| 83 | + Assert.Throws<ArgumentNullException>(() => buf.GetPointer(null!)); | ||
| 84 | + | ||
| 85 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([0, 0])); | ||
| 86 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([0, 0, 0, 0])); | ||
| 87 | + | ||
| 88 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([-1, 0, 0])); | ||
| 89 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([0, -1, 0])); | ||
| 90 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([0, 0, -1])); | ||
| 91 | + | ||
| 92 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([10, 0, 0])); | ||
| 93 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([0, 10, 0])); | ||
| 94 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([0, 0, 10])); | ||
| 95 | + | ||
| 96 | + using PyBuffer buf2 = ndArray.GetBuffer(PyBUF.ND); | ||
| 97 | + | ||
| 98 | + Assert.Throws<InvalidOperationException>( | ||
| 99 | + () => buf2.GetPointer([0, 0, 0]) | ||
| 100 | + ); | ||
| 77 | 101 | } | |
| 78 | 102 | ||
| 79 | 103 | [Test] | |
| 80 | 104 | public void ArrayHasBuffer() | |
| 81 | 105 | { | |
| 82 | - var array = new[,] {{1, 2}, {3,4}}; | ||
| 106 | + var array = new[,] { { 1, 2 }, { 3, 4 } }; | ||
| 83 | 107 | var memoryView = PythonEngine.Eval("memoryview"); | |
| 84 | 108 | var mem = memoryView.Invoke(array.ToPython()); | |
| 85 | 109 | Assert.That(mem[(0, 0).ToPython()].As<int>(), Is.EqualTo(1)); | |
@@ -128,7 +152,7 @@ public void Finalization() | |||
| 128 | 152 | [Test] | |
| 129 | 153 | public void MultidimensionalNumPyArray() | |
| 130 | 154 | { | |
| 131 | - var ndarray = np.arange(24).reshape(1,2,3,4).T; | ||
| 155 | + var ndarray = np.arange(24).reshape(1, 2, 3, 4).T; | ||
| 132 | 156 | PyObject ndim = ndarray.ndim; | |
| 133 | 157 | PyObject shape = ndarray.shape; | |
| 134 | 158 | PyObject strides = ndarray.strides; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -110,10 +110,37 @@ public bool IsContiguous(BufferOrderStyle order) | |||
| 110 | 110 | /// </summary> | |
| 111 | 111 | public IntPtr GetPointer(long[] indices) | |
| 112 | 112 | { | |
| 113 | - if (indices is null) throw new ArgumentNullException(nameof(indices)); | ||
| 113 | + if (indices is null) | ||
| 114 | + throw new ArgumentNullException(nameof(indices)); | ||
| 115 | + | ||
| 116 | + if (Strides is null) | ||
| 117 | + throw new InvalidOperationException($"{nameof(GetPointer)} requires {nameof(Strides)} to be filled"); | ||
| 118 | + | ||
| 119 | + if (indices.Length != Dimensions) | ||
| 120 | + throw new ArgumentOutOfRangeException($"{nameof(indices)} must be exactly {Dimensions} long"); | ||
| 121 | + | ||
| 122 | + if (Dimensions == 0 && Shape is null) | ||
| 123 | + throw new InvalidOperationException($"{nameof(GetPointer)} requires {nameof(Shape)} to be filled for dimensional buffers"); | ||
| 124 | + | ||
| 114 | 125 | ThrowIfDisposed(); | |
| 126 | + | ||
| 115 | 127 | if (Runtime.PyVersion < new Version(3, 7)) | |
| 116 | 128 | throw new NotSupportedException("GetPointer requires at least Python 3.7"); | |
| 129 | + | ||
| 130 | + var indicesArr = new nint[Dimensions]; | ||
| 131 | + | ||
| 132 | + for (var i = 0; i < Dimensions; i++) | ||
| 133 | + { | ||
| 134 | + indicesArr[i] = checked((nint)indices[i]); | ||
| 135 | + | ||
| 136 | + if (indicesArr[i] < 0 || indicesArr[i] >= Shape![i]) | ||
| 137 | + { | ||
| 138 | + throw new ArgumentOutOfRangeException( | ||
| 139 | + $"Index {i} has a value of {indicesArr[i]} not in [0, {Shape![i]})" | ||
| 140 | + ); | ||
| 141 | + } | ||
| 142 | + } | ||
| 143 | + | ||
| 117 | 144 | return Runtime.PyBuffer_GetPointer(ref _view, indices.Select(x => checked((nint)x)).ToArray()); | |
| 118 | 145 | } | |
| 119 | 146 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments