RE: [ctypes-users] Variable length arrays
Brought to you by:
theller
From: Andrew M. <And...@ab...> - 2004-02-20 06:29:40
|
The ctypes tutorial page notes that "pointer instances can also be indexed with integers". So this should work: class FMA_Control_t(Structure): _fields_ = [ ...., ("filenames", POINTER(c_char_p)), .... ] ... obj = FMA_Control_t() f1 = obj.filenames[1] # 2nd char* in "array" ... Of course, as with C, you should expect trouble if you don't get the memory bookkeeping correct... ----------------------------------------------------------------------- Andrew MacIntyre \ E-mail: and...@ab... Planning Branch \ Tel: +61 2 6256 2812 Australian Broadcasting Authority \ Fax: +61 2 6253 3277 -> "These thoughts are mine alone!" <---------------------------------- > -----Original Message----- > From: Chris Melville [mailto:pu...@as...] > Sent: Friday, 20 February 2004 4:41 PM > To: cty...@li... > Subject: RE: [ctypes-users] Variable length arrays > > > > Thanks - I will give that at try. > > I'm still confused a bit though, since: > > Filenames is supposed to be a pointer to a (contiguous) list > of pointers > each of which point to a string: > > Root ptr Ptr table Actual string storage > -------- --------- ------------------ > > Filenames -> ptr_str1 -> "string 1\0" > ptr_str2 -> "string 2\0" > etc > | > v > (4-byte contiguous) > > Clearly the pointer table is an array of pointers (to chars), which in > ctypes can be c_char_p * <array size> > > So a pointer to the above should be expressible > POINTER(c_char_p * <array > size>), which does work fine when the required array size is known at > declaration time. > > In C, pointers and arrays are more or less interchangeable, > but I'm not sure > if that's the case in ctypes too - I will try what you > suggest and get back. > > Thanks again.. > > > -----Original Message----- > From: Bob Ippolito [mailto:bo...@re...] > Sent: Friday, 20 February 2004 12:52 PM > To: Chris Melville > Cc: cty...@li... > Subject: Re: [ctypes-users] Variable length arrays > > That just looks wrong to me.. shouldn't it be simply: > .... > ("filenames", POINTER(c_char_p)), > ] > obj = FMA_Control_t() > obj.filenames = (c_char_p * len(myfilenames))(*myfilenames) > > c_char_p is a "char *" in C.. a single string. > POINTER(c_char_p) is a > "char **" in C, which can be an array of strings.. no need to declare > how many there are in the structure. > > -bob > > On Feb 19, 2004, at 11:11 PM, Chris Melville wrote: > > > > > Thanks but I already said that the structure ISN'T variable > length. It > > has a > > (fixed size) member which points to an array of c_char_p's, which is > > declared as a fixed length array (because ctypes requires that), ie: > > > > class FMA_Control_t(Structure): > > > > _fields_ = [ > > .... > > > > , ("filenames", POINTER(c_char_p * 1)) > > > > .... > > ] > > > > .... > > > > obj = FMA_Control_t() > > > > .... > > > > > > Later on I find that I have (say) 20 filenames to be put into > > "filenames" > > member (ie. into the string array that it points to). > > > > So I then want to change (or resize) obj.filenames to now refer to a > > POINTER(c_char_p * 20). > > > > But I can't find a way to do it!! > > > > I have a feeling I'm going to be going "D'OH" at some point because > > there > > surely must be an easy way to do this!! > > > > > > > > -----Original Message----- > > From: Bob Ippolito [mailto:bo...@re...] > > Sent: Friday, 20 February 2004 11:23 AM > > To: Chris Melville > > Cc: cty...@li... > > Subject: Re: [ctypes-users] Variable length arrays > > > > You said variable length array, not variable length > structure.. That's > > a different matter altogether. Such structures don't exist > in C.. they > > are read incrementally, I suggest you do the same until > ctypes supports > > this use case, if ever. > > > > I have a project that deals with variable length structures called > > ptypes (because it's remotely similar API-wise to ctypes) for > > reading/writing data formats. However, since it's for > reading/writing > > from disk/network, and it's probably very slow at the > moment b/c it has > > no optimizations, it probably isn't what you want. > > > > If you want to take a look at it (early, but working and relatively > > well tested), I have made a snapshot at > > http://undefined.org/python/ptypes-20040219.tgz -- you will need to > > install a VERY RECENT (as in hours ago) CVS version of > PyProtocols if > > you want to play with it. > > > > -bob > > > > > > On Feb 19, 2004, at 9:58 PM, Chris Melville wrote: > > > >> > >> Thanks Bob, but I must be missing something (or I didn't explain it > >> very > >> well). > >> > >>> nothing is stopping you from creating the type itself > >>> as soon as you know how big it needs to be. > >> > >> That's the problem - it is! I need to define a structure, > one member > >> of > >> which is the array in question (well its actually a > pointer to string > >> array) > >> at startup. However I can't find a way (without getting > into low-level > >> buffer horseplay) to then resize the array when it has to > be populated > >> (to > >> some previously unknown size) later on. > >> > >> I was thinking that it would be nice if it was possible to > >> "re-initialize" > >> an array based on specification of a tuple or list, ie. passing a > >> tuple of > >> strings to some API would set the array (assumed to be a c_char_p * > >> <some > >> number>) to be populated by the contents of that tuple (or list), > >> adjusting > >> it's length as necessary. > >> > >> I tried to write one (in Python) to do just that, but there didn't > >> seem to > >> be any obvious way to accomplish it. > >> > >> But, as I said, I might be missing something... > >> > >> > >> -----Original Message----- > >> From: Bob Ippolito [mailto:bo...@re...] > >> Sent: Friday, 20 February 2004 10:06 AM > >> To: Chris Melville > >> Cc: cty...@li... > >> Subject: Re: [ctypes-users] Variable length arrays > >> > >> On Feb 19, 2004, at 8:02 PM, Chris Melville wrote: > >> > >>> Is it possible to do variable length arrays in ctypes, ie. arrays > >>> whose size > >>> isn't known until run-time? > >> > >> > >>> As far as I can see so far, arrays have to be declared of a fixed > >>> size, and > >>> this can't be changed later. > >> > >> That's how they are in C too, unless you're talking about using > >> realloc. Nothing is stopping you from creating a bigger array, and > >> then copying the contents of your existing array... which is > >> essentially what realloc does in the worst case. > >> > >> I suppose that you could use realloc, but you'd have to do a lot of > >> magic to deal with that from ctypes. > >> > >> -bob > >> > >> > >> > >> ------------------------------------------------------- > >> SF.Net is sponsored by: Speed Start Your Linux Apps Now. > >> Build and deploy apps & Web services for Linux with > >> a free DVD software kit from IBM. Click Now! > >> http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click > >> _______________________________________________ > >> ctypes-users mailing list > >> cty...@li... > >> https://lists.sourceforge.net/lists/listinfo/ctypes-users > > > > > > > > ------------------------------------------------------- > > SF.Net is sponsored by: Speed Start Your Linux Apps Now. > > Build and deploy apps & Web services for Linux with > > a free DVD software kit from IBM. Click Now! > > http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click > > _______________________________________________ > > ctypes-users mailing list > > cty...@li... > > https://lists.sourceforge.net/lists/listinfo/ctypes-users > > > > ------------------------------------------------------- > SF.Net is sponsored by: Speed Start Your Linux Apps Now. > Build and deploy apps & Web services for Linux with > a free DVD software kit from IBM. Click Now! > http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click > _______________________________________________ > ctypes-users mailing list > cty...@li... > https://lists.sourceforge.net/lists/listinfo/ctypes-users > |