Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,4 @@
- ([@alxnull](https://github.com/alxnull))
- ([@gpetrou](https://github.com/gpetrou))
- Ehsan Iran-Nejad ([@eirannejad](https://github.com/eirannejad))
- ([@legomanww](https://github.com/legomanww))
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- Make a second call to `pythonnet.load` a no-op, as it was intended.

- Added support for multiple inheritance when inheriting from a class and/or multiple interfaces.
- Fixed error occuring when calling `GetBuffer` for anything other than `PyBUF.SIMPLE`

## [3.0.1](https://github.com/pythonnet/pythonnet/releases/tag/v3.0.1) - 2022-11-03

Expand Down
36 changes: 36 additions & 0 deletions src/embed_tests/TestPyBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ public void Finalization()
Assert.AreEqual(1, arr.Refcount);
}

[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)
{
Expand All @@ -121,5 +141,21 @@ 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;
}
}
}
}
}
8 changes: 4 additions & 4 deletions src/runtime/PythonTypes/PyBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public sealed class PyBuffer : IDisposable
private PyObject _exporter;
private Py_buffer _view;

unsafe internal PyBuffer(PyObject exporter, PyBUF flags)
internal PyBuffer(PyObject exporter, PyBUF flags)
{
_view = new Py_buffer();

Expand All @@ -25,17 +25,17 @@ unsafe internal PyBuffer(PyObject exporter, PyBUF flags)
var intPtrBuf = new IntPtr[_view.ndim];
if (_view.shape != IntPtr.Zero)
{
Marshal.Copy(_view.shape, intPtrBuf, 0, (int)_view.len * sizeof(IntPtr));
Marshal.Copy(_view.shape, intPtrBuf, 0, _view.ndim);
Shape = intPtrBuf.Select(x => (long)x).ToArray();
}

if (_view.strides != IntPtr.Zero) {
Marshal.Copy(_view.strides, intPtrBuf, 0, (int)_view.len * sizeof(IntPtr));
Marshal.Copy(_view.strides, intPtrBuf, 0, _view.ndim);
Strides = intPtrBuf.Select(x => (long)x).ToArray();
}

if (_view.suboffsets != IntPtr.Zero) {
Marshal.Copy(_view.suboffsets, intPtrBuf, 0, (int)_view.len * sizeof(IntPtr));
Marshal.Copy(_view.suboffsets, intPtrBuf, 0, _view.ndim);
SubOffsets = intPtrBuf.Select(x => (long)x).ToArray();
}
}
Expand Down