Resize ctypes array
I'd like to resize a ctypes array. As you can see, ctypes.resize doesn't work like it could. I can write a function to resize an array, but I wanted to know some other solutions to this. Maybe I'm missing some ctypes trick or maybe I simply used resize wrong. The name c_long_Array_0 seems to tell me this may not work like I want. What is resize meant for? >>> from ctypes import * >>> c_int * 0 >>> intType = c_int * 0 >>> foo = intType() >>> foo <__main__.c_long_Array_0 object at 0xb7ed9e84> >>> foo[0] Traceback (most recent call last): File "", line 1, in IndexError: invalid index >>> resize(foo, sizeof(c_int * 1)) >>> foo[0] Traceback (most recent call last): File "", line 1, in IndexError: invalid index >>> foo <__main__.c_long_Array_0 object at 0xb7ed9e84> Maybe go with something like: >>> ctypes_resize = resize >>> def resize(arr, type): ... tmp = type() ... for i in range(len(arr)): ... tmp[i] = arr[i] ... return tmp ... ... >>> listType = c_int * 0 >>> list = listType() >>> list = resize(list, c_int * 1) >>> list[0] 0 >>> But that's ugly passing the type instead of the size. It works for its purpose and that's it. -- http://mail.python.org/mailman/listinfo/python-list
Re: Resize ctypes array
I found an answer to this over on Stackoverflow. http://stackoverflow.com/questions/919369/resize-ctypes-array On Thu, May 28, 2009 at 1:25 AM, Scott Sibley wrote: > I'd like to resize a ctypes array. As you can see, ctypes.resize doesn't > work like it could. I can write a function to resize an array, but I wanted > to know some other solutions to this. Maybe I'm missing some ctypes trick or > maybe I simply used resize wrong. The name c_long_Array_0 seems to tell me > this may not work like I want. What is resize meant for? > > >>> from ctypes import * > >>> c_int * 0 > > > >>> intType = c_int * 0 > >>> foo = intType() > >>> foo > <__main__.c_long_Array_0 object at 0xb7ed9e84> > >>> foo[0] > Traceback (most recent call last): > > > File "", line 1, in > > IndexError: invalid index > >>> resize(foo, sizeof(c_int * 1)) > > >>> foo[0] > Traceback (most recent call last): > > > File "", line 1, in > > IndexError: invalid index > >>> foo > <__main__.c_long_Array_0 object at 0xb7ed9e84> > > > Maybe go with something like: > > >>> ctypes_resize = resize > >>> def resize(arr, type): > > ... tmp = type() > ... for i in range(len(arr)): > > ... tmp[i] = arr[i] > ... return tmp > ... > ... > >>> listType = c_int * 0 > >>> list = listType() > >>> list = resize(list, c_int * 1) > > >>> list[0] > 0 > >>> > > > But that's ugly passing the type instead of the size. It works for its > purpose and that's it. > > -- http://mail.python.org/mailman/listinfo/python-list
Ctypes to wrap libusb-1.0
I have been having issues trying to wrap libusb-1.0 with ctypes. Actually, there's not much of an issue if I keep everything synchronous, but I need this to be asynchronous and that is where my problem arises. Please refer to the following link on Stackoverflow for a full overview of the issue. http://stackoverflow.com/questions/1060305/usb-sync-vs-async-vs-semi-async-partially-answered-now As mentioned in the question on Stackoverflow, synchronous transfers work great. I wrote an asynchronous C version that works fine. usbmon's output suggests the transfers are making it through. libusb's debug output shows nothing out of the ordinary. I've also asked about this on the libusb mailing list. I've hesitated asking on the Python mailing list up till now. -- http://mail.python.org/mailman/listinfo/python-list