Re: [ctypes-users] copying/slicing ctypes arrays, (c_ulong *n)() - to Numeric
Brought to you by:
theller
From: Ray S <ra...@bl...> - 2005-02-03 20:22:08
|
At 07:32 PM 2/3/2005 +0100, you wrote: >Don't be confused that the buffer() object says <read-only buffer ...>! >The buffer call only asks for readable memory..., but ctypes doesn't >care about the readonly attribute - it will happily write into this >memory. Hi Thomas, Yes, I was thinking of what the shell error said upon assignment... Upon adding to some working code all is well: >>> import Numeric, ctypes, string >>> N = Numeric.zeros((10,), Numeric.Float) >>> buf = buffer(N) >>> buf <read-only buffer for 0x008F9C28, ptr 0x008D7780, size 80 at 0x008FE220> >>> int(string.split(repr(buf))[5][:-1], 16) 9271168 ## numarray version # nAddress = int(string.split(repr(N._data))[2], 16) ## Numeric version NAddress = int(string.split(repr(buffer(N)))[5][:-1], 16) ## Load DLL here... ## do this to get data from the USB A/D's DLL usb.GetData(usb.Sn, (bufferInsertPos * N.itemsize()) + NAddress, ctypes.byref( (types.c_long * buffersize)() ) ) Which is faster than getting data into a ctypes array (c_ulong *n)() and then doing memmove() to Numeric - one less step. Maybe this snip would be of help to some others, although more so to numpy people. Of course, the Python array works the same: >>> a = array.array('l',[1,2,3]) >>> int(string.split(repr(buffer(a)))[5][:-1], 16) 8380408 >If this is too confusing, and this may well be, ctypes could expose a >memory() function which would insist on read-write memory, but apart >from that do the same that buffer does: No, not confusing, just not clear to a non-expert C person that ctypes ignores where Python is read-only. A simple note in the tutorial would be fine. Some over at numpy were also unaware of memmove()s' existence in the new releases, and seemed interested. Thanks again, Ray |