| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -85,3 +85,4 @@ | |||
| 85 | 85 | - ([@alxnull](https://github.com/alxnull)) | |
| 86 | 86 | - ([@gpetrou](https://github.com/gpetrou)) | |
| 87 | 87 | - Ehsan Iran-Nejad ([@eirannejad](https://github.com/eirannejad)) | |
| 88 | + - ([@legomanww](https://github.com/legomanww)) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,6 +17,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][]. | |||
| 17 | 17 | - Make a second call to `pythonnet.load` a no-op, as it was intended. | |
| 18 | 18 | ||
| 19 | 19 | - Added support for multiple inheritance when inheriting from a class and/or multiple interfaces. | |
| 20 | + - Fixed error occuring when calling `GetBuffer` for anything other than `PyBUF.SIMPLE` | ||
| 20 | 21 | ||
| 21 | 22 | ## [3.0.1](https://github.com/pythonnet/pythonnet/releases/tag/v3.0.1) - 2022-11-03 | |
| 22 | 23 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -110,6 +110,26 @@ public void Finalization() | |||
| 110 | 110 | Assert.AreEqual(1, arr.Refcount); | |
| 111 | 111 | } | |
| 112 | 112 | ||
| 113 | + [Test] | ||
| 114 | + public void MultidimensionalNumPyArray() | ||
| 115 | + { | ||
| 116 | + var ndarray = np.arange(24).reshape(1,2,3,4).T; | ||
| 117 | + PyObject ndim = ndarray.ndim; | ||
| 118 | + PyObject shape = ndarray.shape; | ||
| 119 | + PyObject strides = ndarray.strides; | ||
| 120 | + PyObject contiguous = ndarray.flags["C_CONTIGUOUS"]; | ||
| 121 | + | ||
| 122 | + using PyBuffer buf = ndarray.GetBuffer(PyBUF.STRIDED); | ||
| 123 | + | ||
| 124 | + Assert.Multiple(() => | ||
| 125 | + { | ||
| 126 | + Assert.That(buf.Dimensions, Is.EqualTo(ndim.As<int>())); | ||
| 127 | + Assert.That(buf.Shape, Is.EqualTo(shape.As<long[]>())); | ||
| 128 | + Assert.That(buf.Strides, Is.EqualTo(strides.As<long[]>())); | ||
| 129 | + Assert.That(buf.IsContiguous(BufferOrderStyle.C), Is.EqualTo(contiguous.As<bool>())); | ||
| 130 | + }); | ||
| 131 | + } | ||
| 132 | + | ||
| 113 | 133 | [MethodImpl(MethodImplOptions.NoInlining)] | |
| 114 | 134 | static void MakeBufAndLeak(PyObject bufProvider) | |
| 115 | 135 | { | |
@@ -121,5 +141,21 @@ static PyObject ByteArrayFromAsciiString(string str) | |||
| 121 | 141 | using var scope = Py.CreateScope(); | |
| 122 | 142 | return Runtime.Runtime.PyByteArray_FromStringAndSize(str).MoveToPyObject(); | |
| 123 | 143 | } | |
| 144 | + | ||
| 145 | + dynamic np | ||
| 146 | + { | ||
| 147 | + get | ||
| 148 | + { | ||
| 149 | + try | ||
| 150 | + { | ||
| 151 | + return Py.Import("numpy"); | ||
| 152 | + } | ||
| 153 | + catch (PythonException) | ||
| 154 | + { | ||
| 155 | + Assert.Inconclusive("Numpy or dependency not installed"); | ||
| 156 | + return null; | ||
| 157 | + } | ||
| 158 | + } | ||
| 159 | + } | ||
| 124 | 160 | } | |
| 125 | 161 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,7 +11,7 @@ public sealed class PyBuffer : IDisposable | |||
| 11 | 11 | private PyObject _exporter; | |
| 12 | 12 | private Py_buffer _view; | |
| 13 | 13 | ||
| 14 | - unsafe internal PyBuffer(PyObject exporter, PyBUF flags) | ||
| 14 | + internal PyBuffer(PyObject exporter, PyBUF flags) | ||
| 15 | 15 | { | |
| 16 | 16 | _view = new Py_buffer(); | |
| 17 | 17 | ||
@@ -25,17 +25,17 @@ unsafe internal PyBuffer(PyObject exporter, PyBUF flags) | |||
| 25 | 25 | var intPtrBuf = new IntPtr[_view.ndim]; | |
| 26 | 26 | if (_view.shape != IntPtr.Zero) | |
| 27 | 27 | { | |
| 28 | - Marshal.Copy(_view.shape, intPtrBuf, 0, (int)_view.len * sizeof(IntPtr)); | ||
| 28 | + Marshal.Copy(_view.shape, intPtrBuf, 0, _view.ndim); | ||
| 29 | 29 | Shape = intPtrBuf.Select(x => (long)x).ToArray(); | |
| 30 | 30 | } | |
| 31 | 31 | ||
| 32 | 32 | if (_view.strides != IntPtr.Zero) { | |
| 33 | - Marshal.Copy(_view.strides, intPtrBuf, 0, (int)_view.len * sizeof(IntPtr)); | ||
| 33 | + Marshal.Copy(_view.strides, intPtrBuf, 0, _view.ndim); | ||
| 34 | 34 | Strides = intPtrBuf.Select(x => (long)x).ToArray(); | |
| 35 | 35 | } | |
| 36 | 36 | ||
| 37 | 37 | if (_view.suboffsets != IntPtr.Zero) { | |
| 38 | - Marshal.Copy(_view.suboffsets, intPtrBuf, 0, (int)_view.len * sizeof(IntPtr)); | ||
| 38 | + Marshal.Copy(_view.suboffsets, intPtrBuf, 0, _view.ndim); | ||
| 39 | 39 | SubOffsets = intPtrBuf.Select(x => (long)x).ToArray(); | |
| 40 | 40 | } | |
| 41 | 41 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments