ctypes question about call by reference

O

Oliver Andrich

Hi,

hopefully someone with some ctypes experience can help me. I guess
this is a trivial task again, but I have been googling, reading,
experimenting the whole afternoon without any success.

I have a given C function signature:

char *MagickGetException(MagickWand *wand,ExceptionType *severity)

- ExceptionType is an enum
- MagickWand is somewhat strange, but so far it works fine without any
type mangling.

How would I wrap this thing using ctypes? Can anybody help me with that?

Best regards,
Oliver
 
L

Lawrence Oluyede

Oliver Andrich said:
- ExceptionType is an enum
- MagickWand is somewhat strange, but so far it works fine without any
type mangling.

How would I wrap this thing using ctypes? Can anybody help me with that?

First thing first: you have to identify how ExceptionType and MagickWand
are composed of. Then you can start wrapping them following the
instructions in the tutorial:
http://docs.python.org/lib/ctypes-ctypes-tutorial.html
 
O

Oliver Andrich

First thing first: you have to identify how ExceptionType and MagickWand
are composed of. Then you can start wrapping them following the
instructions in the tutorial:
http://docs.python.org/lib/ctypes-ctypes-tutorial.html

Well, what I learned so far from the documentation, which I already
have read more then once today, is that there is no example for an
enum in this situation. But looking at pygame-sdl and another project,
it looks like the enum is just an c_int or c_long.

And concerning MagickWand, I think I have learned, that I don't need
to define the structure, if I don't want to access it. And I don't
want to, cause this is handled by the library itself. Another point in
this context is also, that it is not defined in the header files
distributed with the library, but only in the source from which the
library is built. As the file is named magick-wand-private.h it is
also not meant to be edited.

Is it at all possbile to use a struct without defining it with ctypes?
Is it okay, to just use the int which is returned by default? Most of
my library works with that configuration, so I thought it is working.

Based on this, can you give me some more hints?

Best regards,
Oliver
 
L

Lawrence Oluyede

Oliver Andrich said:
Well, what I learned so far from the documentation, which I already
have read more then once today, is that there is no example for an
enum in this situation. But looking at pygame-sdl and another project,
it looks like the enum is just an c_int or c_long.

The documentation explains how to build an enum to pass to a function:
http://docs.python.org/lib/ctypes-structures-unions.html

See the first sentence.

class ExceptionType(Union):
_fields_ = [list_of_tuples]
Is it at all possbile to use a struct without defining it with ctypes?

If you want to use it you have to define it somewhere...
Is it okay, to just use the int which is returned by default? Most of
my library works with that configuration, so I thought it is working.

I see your functions returns a "char *" so I don't understand what are
you saying here...
Based on this, can you give me some more hints?

It's not really clear :)
 
O

Oliver Andrich

After a walk outside, some fresh air around my nose and some time to
relax, I finally found out how to do, what I want to do.

First, define the argument types and the result type.
_dll.MagickGetException.argtypes = (c_long, POINTER(c_long))
_dll.MagickGetException.restype = c_char_p

Second, write correct code as it is documented. :)

def get_magick_exception(wand):
severity = c_long()
description = _dll.MagickGetException(wand, byref(severity))
return (severity, description)

And thats all. I just did the call to POINTER(c_long) in the wrong
location. Now I do it as documented, and I get everything I want.

I can btw live perfectly without definining what actually a MagickWand
is, cause from the point of the developer just using the library and
documentation, he doesn't know about the actual structure of it. The
definition is hidden the C source of the library, and is not
documented in the public interface.

Thanks and Regards,
Oliver.
 
G

Gabriel Genellina

If you want to use it you have to define it somewhere...

If it's an "opaque" thing, totally managed by the external code, yes
- treat it as a pointer.
That is, if you never have to access its fields, or create/destroy it
in the "python" code (using an external function would be ok).



Gabriel Genellina
Softlab SRL





__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top