ctypes question about call by reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Oliver Andrich

    #1

    ctypes question about call by reference

    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 *MagickGetExcep tion(MagickWand *wand,Exception Type *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

    --
    Oliver Andrich <oliver.andrich @gmail.com--- http://roughbook.de/
  • Lawrence Oluyede

    #2
    Re: ctypes question about call by reference

    Oliver Andrich <oliver.andrich @gmail.comwrote :
    - 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:


    --
    Lawrence - http://www.oluyede.org/blog
    "Nothing is more dangerous than an idea
    if it's the only one you have" - E. A. Chartier

    Comment

    • Oliver Andrich

      #3
      Re: Re: ctypes question about call by reference

      On 9/24/06, Lawrence Oluyede <rhymes@myself. comwrote:
      Oliver Andrich <oliver.andrich @gmail.comwrote :
      - 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
      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


      --
      Oliver Andrich <oliver.andrich @gmail.com--- http://roughbook.de/

      Comment

      • Lawrence Oluyede

        #4
        Re: ctypes question about call by reference

        Oliver Andrich <oliver.andrich @gmail.comwrote :
        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:


        See the first sentence.

        class ExceptionType(U nion):
        _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 :-)

        --
        Lawrence - http://www.oluyede.org/blog
        "Nothing is more dangerous than an idea
        if it's the only one you have" - E. A. Chartier

        Comment

        • Oliver Andrich

          #5
          Re: Re: ctypes question about call by reference

          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.MagickGetE xception.argtyp es = (c_long, POINTER(c_long) )
          _dll.MagickGetE xception.restyp e = c_char_p

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

          def get_magick_exce ption(wand):
          severity = c_long()
          description = _dll.MagickGetE xception(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.

          --
          Oliver Andrich <oliver.andrich @gmail.com--- http://roughbook.de/

          Comment

          • Gabriel Genellina

            #6
            Re: ctypes question about call by reference

            At Sunday 24/9/2006 15:49, Lawrence Oluyede wrote:
            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...
            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!


            Comment

            Working...