ctypes question about call by reference

Discussion in 'Python' started by Oliver Andrich, Sep 24, 2006.

  1. 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 Andrich, Sep 24, 2006
    1. Advertisements

  2. 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:
    Lawrence Oluyede, Sep 24, 2006
    1. Advertisements

  3. 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 Andrich, Sep 24, 2006
  4. The documentation explains how to build an enum to pass to a function:

    See the first sentence.

    class ExceptionType(Union):
    _fields_ = [list_of_tuples]
    If you want to use it you have to define it somewhere...
    I see your functions returns a "char *" so I don't understand what are
    you saying here...
    It's not really clear :)
    Lawrence Oluyede, Sep 24, 2006
  5. 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 Andrich, Sep 24, 2006
  6. 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!
    Gabriel Genellina, Sep 26, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.