PyBuffer.GetPointer throws exception for N-dimensional buffer #2743
Copy link
Copy link
Closed
Labels
Description
PyBuffer.GetPointer throws System.ExecutionEngineException when called with an N-dimensional array obtained with PyBUF.ND flag.
dynamic np = Py.Import("numpy");
// create 3D NumPy array
int[] shape = [10, 10, 10];
PyObject ndArray = np.zeros(shape);
PyBuffer buf = ndArray.GetBuffer(PyBUF.ND);
// this throws System.ExecutionEngineException
nint ptr = buf.GetPointer([0, 0, 0]);I suspect that this may be caused by a mismatch of a type. The following code seems to pass a CLR array to native function pointer.
// src/runtime/Runtime.Delegates.cs
PyBuffer_GetPointer = (delegate* unmanaged[Cdecl]<ref Py_buffer, nint[], IntPtr>)GetFunctionByName(nameof(PyBuffer_GetPointer), GetUnmanagedDll(_PythonDll));The second argument should be nint* obtained by fixed statement, isn't it?
My environment:
- OS: Windows 11
- Python: 3.14
- Python.NET: 3.1.0
- .NET 10
- Visual Studio 2026
Reactions are currently unavailable
Activity
filmor commentedon Sep 28, 2026
I can confirm that I also see crashes in this function, but the proposed fix (
fixedandnint*) does not seem to be enough. Will check further.filmor commentedon Sep 28, 2026
I can't find any useful docs on this, but the issue is
PyBUF.ND:https://github.com/python/cpython/blob/9e9c3c49b4a41eebb982891c3ccdc17ae5ab9963/Objects/abstract.c#L544-L557
PyObject_GetPointeraccessesview->strides, so you must use at leastPyBUF.STRIDESto access it. I will introduce an exception for this onPyBuffer.GetPointer.