python >> ctypes: Get full contents of character array

by overdrigzed » Sat, 13 Sep 2008 07:38:12 GMT

Hello!

I wanted to get the full contents of a character array stored in a
struct, i.e.
_fields_ = [...("array", c_char * 12)...]
however, ctypes seems to try to return struct.array as a Python string
rather than a character array, and stops as soon as it encounters a
null within the character array.

I ended up having to define a dummy struct
class dummystruct(Structure):
_fields_ = []

and declare array as:
("array", dummystruct)

then use string_at(byref(struct.array), 12).

Is this really the best way of doing it? Is there no better way to
work around ctypes 'guess what you want' behaviour?

Thanks in advance,
Rodrigo

python >> ctypes: Get full contents of character array

by Aaron "Castironpi"Brady » Sat, 13 Sep 2008 13:45:52 GMT



> _fields_ = []> >> > and declare array as:> > ("array", dummystruct)> >> > then use string_at(byref(struct.array), 12).> >> > Is this really the best way of doing it? Is there no better way to> > work around ctypes 'guess what you want' behaviour?> >> > Thanks in advance,> > Rodrigo

Rodrigo,

If you have the option to change your declaration to c_byte* 12, you
have more options. This example prints the null character you wanted.

from ctypes import *
import struct
class S( Structure ):
_fields_= [
( 'array', c_byte* 12 )
]
s= S()

#initialize
struct.pack_into( '7s', s.array, 0, 'abc\x00def' )

#prototype and call PyString_FromStringAndSize
prototype= PYFUNCTYPE( py_object, POINTER( c_byte ), c_size_t )
PyString_FromStringAndSize= prototype( ( "PyString_FromStringAndSize",
pythonapi ) )
x= PyString_FromStringAndSize( s.array, 12 )
print repr( x )

#prototype and call PyString_FromString for contrast
prototype= PYFUNCTYPE( py_object, POINTER( c_byte ) )
PyString_FromString= prototype( ( "PyString_FromString", pythonapi ) )
x= PyString_FromString( s.array )
print repr( x )

/Output:

'abc\x00def\x00\x00\x00\x00\x00'
'abc'

python >> ctypes: Get full contents of character array

by overdrigzed » Mon, 15 Sep 2008 01:41:13 GMT

On Sep 13, 6:45m, "Aaron \"Castironpi\" Brady"


> > _fields_ = []> >> > > and declare array as:> > > ("array", dummystruct)> >> > > then use string_at(byref(struct.array), 12).> >> > > Is this really the best way of doing it? Is there no better way to> > > work aroundctypes'guess what you want' behaviour?> >> > > Thanks in advance,> > > Rodrigo> >> > Rodrigo,> >> > If you have the option to change your declaration to c_byte* 12, you> > have more options. his example prints the null character you wanted.> >> > fromctypesimport *> > import struct> > class S( Structure ):> > _fields_=>[
> ( 'array', c_byte> 12 ) >> ]
>
> #in>tialize
> struct.pack_into( '7s', s.array, 0, 'abc\x>0d>f' )
>
> #prototype and call PyString_FromStrin>AndSize
> prototype= PYFUNCTYPE( py_object, POINTER( c_byte ), c_>ize_t )
> PyString_FromStringAndSize= prototype( ( "PyString_FromStringA>dSize",
> pytho>api ) )
> x= PyString_FromStringAndSize( s.arr>y, 12 )
> print r>pr> x )
>
> #prototype and call PyString_FromString for >ontrast
> prototype= PYFUNCTYPE( py_object, POINTER( c_>yte ) )
> PyString_FromString= prototype( ( "PyString_FromString", pytho>api ) )
> x= PyString_FromString( s>array )
> print r>pr> x )
>
> >Ou>put:
>
> 'abc\x00def\x00\x00\x00\>00\x00'
> 'abc'

Great - and thank you for replying so fast! I found that [chr(x) for x
in struct.array] with array as a c_byte * 12 did what I needed.

Similar Threads

1. ctypes help for "array of character pointers" as an output parameter

2. getting full exception information from an xmlrpc server

I'm debugging an xmlrpc client/server application. Often when an
exception occurs in the server, I receive only a very short error
message on the client. For example:

    xmlrpclib.Fault: <Fault 1: "<type 'exceptions.AssertionError'>:">

Presumably this is because xmlrpclib on the server is catching the
exception, and only sending the exception name to the client, not the
server's stack trace.

What I would like is the full stack trace of what went wrong on the
server (i.e. the junk python usually dumps to the console when an
exception occurs). I don't care which side I see the dump on (client
or server), but I need to see the whole trace so I can figure out what
the problem is. Is there an easy way to do this?

Thanks,
Scott

3. Embedding - getting full error string

4. Finding the array index number for known content.

Hi Folks,

If I have an array holding a pair of numbers, and that pairing is
unique, is there a way that I can find the array index number for that
pair?

Thanks,

PhilC

5. reading file contents to an array (newbie)

6. trouble to print array contents using slice operator

7. Need help getting BeautifulSoup contents

8. regular expression for getting content between parentheses

Hi

I have a text file as follows:

testName = (
 someParam = value1
 anotherParam = (value2, value3)
)

how do I write a regular expression to get all the contents of the
file which are between the first and last parentheses.

In this case,  I want:

 someParam = value1
 anotherParam = (value2, value3)

Thanks,

Raj