logo       

RE: cast function, other ideas: msg#00013

python.ctypes

Subject: RE: cast function, other ideas

Hi Thomas,

Thanks again for Ctypes.

Could it be made to look like C++ (a constructor call)?
my_c_char_p = c_char_p(ptr)

For the get_string() function I think it's functionality would be
perfect if it also included an optional length parameter. I have
found that some of the 'strings' I get back from API's are not
null terminated - but defined by length.

Would you need an equivalent get_wstring()?

Just my thoughts, and thanks for the excellent tool
Mark

P.S. I just added Decompress functionality to your cabinet compression
utility.
I have only just hacked it on based closely on your original code.
I think that the only extra thing that the decompression part shows is
the use
of a call back with a pointer to a structure (which took me about 10
tried
before I got it right)



-----Original Message-----
From: ctypes-users-admin@xxxxxxxxxxxxxxxxxxxxx
[mailto:ctypes-users-admin@xxxxxxxxxxxxxxxxxxxxx] On Behalf Of Thomas
Heller
Sent: Tuesday, October 12, 2004 4:20 PM
To: ctypes-users@xxxxxxxxxxxxxxxxxxxxx
Subject: [ctypes-users] cast function, other ideas


I recently did some experiments wrapping parts of libxml2.

I started with a pyrex module implemented by Martijn Faassen, and
converted it to ctypes code. Mostly it was pretty straightforward, and
the libxml header file has already been 'converted' to ctypes
definitions by my brand new gccxml/parser/code generator combo (more on
that later, in another post).

One problem was that libxml represents string pointers by
'unsigned byte *', which in ctypes is spelled POINTER(c_ubyte).

For string pointers, ctypes has the c_char_p type, and I didn't want to
hack on the generated wrapper files, so I came up with a cast function.

cast(ptr, c_char_p) returns a c_char_p instance, pointing to the same
address as the ptr instance (an instance of POINTER(c_ubyte)).
c_char_p has a .value attribute, which allows to access the nul
terminated string pointed to. IMO, the cast() function is a better way
to access the string at that address, instead of implementing .value
attributes for all the POINTER(byte) types.

Further, I think a get_string() function, which also takes a POINTER
instance (or even an integer address) as parameter would be useful in
ctypes, to make it easier to get these strings. With help of cast, it
can be implemented in this way, although I would prefer to implement it
in C to make it more efficient:

def get_string(ptr):
return cast(ptr, c_char_p).value


While we're on it, other functions that would be useful may be these
(iirc, Paul Moore had suggested something like this long time ago):
memcpy, strcpy, wcscpy.

All of these functions may be very useful, escpecially to use generated
code, where there's no way to fix all the wrappers for exactly the
argtypes and restype one really needs.

Any thoughts?

Thomas



-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal
Use IT products in your business? Tell us what you think of them. Give
us
Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out
more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
ctypes-users mailing list
ctypes-users@xxxxxxxxxxxxxxxxxxxxx
https://lists.sourceforge.net/lists/listinfo/ctypes-users

Attachment: CreateCab.py
Description: CreateCab.py

<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise