以下copy a .NET array to a numpy array的代码在python 2.7.17中有效,但在python 3.7.7中不再有效:
import clr, numpy
from System import Array, Int32, IntPtr
from System.Runtime.InteropServices import Marshal
n = 10
net_arr = Array.CreateInstance(Int32, n)
for i in range(0, n): net_arr[i] = i
np_arr = numpy.zeros([n], int)
np_ptr = IntPtr.__overloads__[int](np_arr.__array_interface__['data'][0])
Marshal.Copy(net_arr, 0, np_ptr, net_arr.Length)
print(np_arr)
# python2 output:
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# python3 output:
Traceback (most recent call last):
File "test.py", line 10, in <module>
np_ptr = IntPtr.__overloads__[int](np_arr.__array_interface__['data'][0])
TypeError: no constructor matches given arguments
我知道python类型从2更改为3,但是搜索并使用构造函数并没有帮助我找到解决方案。感谢您的帮助!
解决方法
([from Python.NET Mailing List),由于直接访问指针并且避免了复制操作,因此速度甚至更快:
import ctypes
from System.Runtime.InteropServices import GCHandle, GCHandleType
net_hndl = GCHandle.Alloc(net_arr, GCHandleType.Pinned)
try:
net_ptr = net_hndl.AddrOfPinnedObject().ToInt64() # ToInt32 gave an Overflow Error
bufType = ctypes.c_int32 * len(net_arr)
cbuf = bufType.from_address(net_ptr)
np_arr = numpy.frombuffer(cbuf, dtype=cbuf._type_)
finally:
if net_hndl.IsAllocated: net_hndl.Free()
但是,它不能解释原始问题。如原始帖子中所述,它可能与python integer changes from version 2 to 3有关,也许与this post有关,但我无法找出有效的IntPtr constructor。
如果有人可以将它组合在一起,我仍然会很感激!