Passing a packed C structure to a c module

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philippe Martin

    #1

    Passing a packed C structure to a c module

    Hi,

    Is it possible to define a packed C structure in python and pass it to the c
    module, or should the wrapper do that ?

    Regards,

    Philippe

  • Martin v. Löwis

    #2
    Re: Passing a packed C structure to a c module

    Philippe Martin wrote:[color=blue]
    > Is it possible to define a packed C structure in python and pass it to the c
    > module, or should the wrapper do that ?[/color]

    You can create a packed structure using string concatenation, and with
    the help of the struct module. However, this gives you a string object
    in the end, which you still need to pass into your C library.

    It is better to fill the struct in the wrapper.

    Regards,
    Martin

    Comment

    • Philippe Martin

      #3
      Re: Passing a packed C structure to a c module

      Thanks,

      It's a pretty big structure: painfull to pass each item as a param.

      Regards,

      Philippe


      "Martin v. Löwis" wrote:
      [color=blue]
      > Philippe Martin wrote:[color=green]
      >> Is it possible to define a packed C structure in python and pass it to
      >> the c module, or should the wrapper do that ?[/color]
      >
      > You can create a packed structure using string concatenation, and with
      > the help of the struct module. However, this gives you a string object
      > in the end, which you still need to pass into your C library.
      >
      > It is better to fill the struct in the wrapper.
      >
      > Regards,
      > Martin[/color]

      Comment

      • Martin v. Löwis

        #4
        Re: Passing a packed C structure to a c module

        Philippe Martin wrote:[color=blue]
        > It's a pretty big structure: painfull to pass each item as a param.[/color]

        So how else would you like to pass them? Define the API you want,
        and then just implement it. It still shouldn't require to define
        the layout in Python.

        Regards,
        Martin

        Comment

        • Philippe Martin

          #5
          Re: Passing a packed C structure to a c module

          Well,

          The call actually is an IOCtl: depending on the control code, the structure
          has a different format.

          Although the number of control codes/structures is finite, it would make the
          wrapper function fairly large.

          You seem to think that building the structure from python would be a
          mistake: why is that ?

          PS: the wrapper also has to work under multiple OSs

          Regards,

          Philippe





          "Martin v. Löwis" wrote:
          [color=blue]
          > Philippe Martin wrote:[color=green]
          >> It's a pretty big structure: painfull to pass each item as a param.[/color]
          >
          > So how else would you like to pass them? Define the API you want,
          > and then just implement it. It still shouldn't require to define
          > the layout in Python.
          >
          > Regards,
          > Martin[/color]

          Comment

          • Jay Parlar

            #6
            Re: Passing a packed C structure to a c module


            On Apr 14, 2006, at 9:44 AM, Philippe Martin wrote:
            [color=blue]
            > Thanks,
            >
            > It's a pretty big structure: painfull to pass each item as a param.
            >
            > Regards,
            >
            > Philippe
            >[/color]

            Maybe this can do something for you?

            Download Construct (data structure definition) for free. Construct is a python module for bit-level, symmetrical parsing and building of data structures. Complex constructs are formed by a hierarchy of less-complex constructs, as well as user-defined constructs.



            Jay P.

            Comment

            • Martin v. Löwis

              #7
              Re: Passing a packed C structure to a c module

              Philippe Martin wrote:[color=blue]
              > The call actually is an IOCtl: depending on the control code, the structure
              > has a different format.[/color]

              Ah. In that case, I recommend to use the ioctl module; you won't need a
              C wrapper, then.
              [color=blue]
              > Although the number of control codes/structures is finite, it would make the
              > wrapper function fairly large.[/color]

              *Some* code to handle this will be very large, regardless of how you
              write it.
              [color=blue]
              > You seem to think that building the structure from python would be a
              > mistake: why is that ?[/color]

              It's very error-prone. You have to match the C structure layout of the
              compiler precisely, and you even have to get the alignment right,
              something that can't be done reliably in Python.
              [color=blue]
              > PS: the wrapper also has to work under multiple OSs[/color]

              That makes it worse: now you not only have to match the C compiler's
              expectation on a single processor, but on different ones.

              Perhaps you should be using ctypes.

              Regards,
              Martin

              Comment

              • Philippe Martin

                #8
                Re: Passing a packed C structure to a c module

                I misslead you: it is a smart card-style ioctl ...

                Philippe



                "Martin v. Löwis" wrote:
                [color=blue]
                > Philippe Martin wrote:[color=green]
                >> The call actually is an IOCtl: depending on the control code, the
                >> structure has a different format.[/color]
                >
                > Ah. In that case, I recommend to use the ioctl module; you won't need a
                > C wrapper, then.
                >[color=green]
                >> Although the number of control codes/structures is finite, it would make
                >> the wrapper function fairly large.[/color]
                >
                > *Some* code to handle this will be very large, regardless of how you
                > write it.
                >[color=green]
                >> You seem to think that building the structure from python would be a
                >> mistake: why is that ?[/color]
                >
                > It's very error-prone. You have to match the C structure layout of the
                > compiler precisely, and you even have to get the alignment right,
                > something that can't be done reliably in Python.
                >[color=green]
                >> PS: the wrapper also has to work under multiple OSs[/color]
                >
                > That makes it worse: now you not only have to match the C compiler's
                > expectation on a single processor, but on different ones.
                >
                > Perhaps you should be using ctypes.
                >
                > Regards,
                > Martin[/color]

                Comment

                • Philippe Martin

                  #9
                  Re: Passing a packed C structure to a c module

                  I'll take a look,

                  thanks.



                  Jay Parlar wrote:
                  [color=blue]
                  >
                  > On Apr 14, 2006, at 9:44 AM, Philippe Martin wrote:
                  >[color=green]
                  >> Thanks,
                  >>
                  >> It's a pretty big structure: painfull to pass each item as a param.
                  >>
                  >> Regards,
                  >>
                  >> Philippe
                  >>[/color]
                  >
                  > Maybe this can do something for you?
                  >
                  > http://pyconstruct.sourceforge.net/
                  >
                  >
                  > Jay P.[/color]

                  Comment

                  Working...