I would like to resize the ctypes array. As you can see, ctypes.resize does not work as it could. I can write a function to resize the array, but I wanted to know some other solutions. Maybe I am missing the ctypes trick or maybe I just used the resizing incorrectly. The name c_long_Array_0 seems to tell me that this may not work with resizing.
>>> from ctypes import *
>>> c_int * 0
<class '__main__.c_long_Array_0'>
>>> intType = c_int * 0
>>> foo = intType()
>>> foo
<__main__.c_long_Array_0 object at 0xb7ed9e84>
>>> foo[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: invalid index
>>> resize(foo, sizeof(c_int * 1))
>>> foo[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: invalid index
>>> foo
<__main__.c_long_Array_0 object at 0xb7ed9e84>
>>> sizeof(c_int * 0)
0
>>> sizeof(c_int * 1)
4
Edit: maybe 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 this is an ugly type walkthrough instead of size. He works for his purpose and that he is.
Scott source
share