Pass a tuple (or list) to a C wrapper function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Java and Swing

    Pass a tuple (or list) to a C wrapper function

    I have a C function which takes an array of long values..

    I understand that I can pass a tuple to a C wrapper function and in the
    C wrapper function have..

    int ok = PyArg_ParseTupl e(args, "s(ll)", &a, &b, &c);

    ...that's great if my tuple only contained two longs..but what if it
    contained 50
    would I have to do..

    int ok = PyArg_ParseTupl e(args, "s(llllllllllll llllll....ll)", &a, &b,
    &c, &d...) ??

    how can I handle this?

  • Jeremy Moles

    #2
    Re: Pass a tuple (or list) to a C wrapper function

    It depends on how you want to manipulate the data in C. If you want
    compile-time variable access to each float, yeah, 50 floats. :)

    Probably what you want to do though is just keep the tuple as is and
    iterate over it using the PySequence_* protocol:



    On Wed, 2005-10-12 at 13:06 -0700, Java and Swing wrote:[color=blue]
    > I have a C function which takes an array of long values..
    >
    > I understand that I can pass a tuple to a C wrapper function and in the
    > C wrapper function have..
    >
    > int ok = PyArg_ParseTupl e(args, "s(ll)", &a, &b, &c);
    >
    > ..that's great if my tuple only contained two longs..but what if it
    > contained 50
    > would I have to do..
    >
    > int ok = PyArg_ParseTupl e(args, "s(llllllllllll llllll....ll)", &a, &b,
    > &c, &d...) ??
    >
    > how can I handle this?
    >[/color]

    Comment

    • Fredrik Lundh

      #3
      Re: Pass a tuple (or list) to a C wrapper function

      Jeremy Moles wrote:
      [color=blue]
      > Probably what you want to do though is just keep the tuple as is and
      > iterate over it using the PySequence_* protocol:
      >
      > http://docs.python.org/api/sequence.html[/color]

      I did post a complete and tested example a few days ago, which contained
      code that showed how to do this. a complete waste of time, of course.

      </F>



      Comment

      • Java and Swing

        #4
        Re: Pass a tuple (or list) to a C wrapper function

        Fredrik...I forgot about that...wish Google Groups had a way to quickly
        find the topics a user posts.

        anyhow, for receiving an object from python..is it

        ok = PyArg_ParseTupl e(args, "sO", &x, &y);

        ....is it "sO" or "s0" ....is it O (as in the letter) or 0 (as in the
        number)? I would think "O" the letter..but it looks like a zero.

        thanks

        Fredrik Lundh wrote:[color=blue]
        > Jeremy Moles wrote:
        >[color=green]
        > > Probably what you want to do though is just keep the tuple as is and
        > > iterate over it using the PySequence_* protocol:
        > >
        > > http://docs.python.org/api/sequence.html[/color]
        >
        > I did post a complete and tested example a few days ago, which contained
        > code that showed how to do this. a complete waste of time, of course.
        >
        > </F>[/color]

        Comment

        • Java and Swing

          #5
          Re: Pass a tuple (or list) to a C wrapper function

          Fredrik,
          ...I tried using your code...

          static long *get_long_array (PyObject *data, int *data_size) {
          int i, size;
          long* out;
          PyObject* seq;

          seq = PySequence_Fast (data, "expected a sequence");
          if (!seq)
          return NULL;

          size = PySequence_Size (seq);
          if (size < 0)
          return NULL;

          if (data_size)
          *data_size = size;

          out = (long*) PyMem_Malloc(si ze * sizeof(long));
          if (!out) {
          Py_DECREF(seq);
          PyErr_NoMemory( );
          return NULL;
          }

          for (i = 0; i < size; i++)
          out[i] = PyInt_AsLong(Py Sequence_Fast_G ET_ITEM(seq, i));

          Py_DECREF(seq);

          if (PyErr_Occurred ()) {
          PyMem_Free(out) ;
          out = NULL;
          }

          return out;

          }


          and I get this error..

          C:\project\myap p.c(549) : error C2040: 'get_long_array ' : 'long
          *(struct _object *,int *)' differs in levels of indirection from 'int
          ()'

          any idea?

          Fredrik Lundh wrote:[color=blue]
          > Jeremy Moles wrote:
          >[color=green]
          > > Probably what you want to do though is just keep the tuple as is and
          > > iterate over it using the PySequence_* protocol:
          > >
          > > http://docs.python.org/api/sequence.html[/color]
          >
          > I did post a complete and tested example a few days ago, which contained
          > code that showed how to do this. a complete waste of time, of course.
          >
          > </F>[/color]

          Comment

          • Java and Swing

            #6
            Re: Pass a tuple (or list) to a C wrapper function

            I got it. I had get_long_array placed after the method that was
            calling it..
            i.e.

            void doStuf(...) {
            x = get_long_array( ...);
            }

            static long *get_long_array (PyObject *data, int *data_size) {
            ....
            }

            ....I put get_long_array before it in my code..and its fine.

            Thanks

            Java and Swing wrote:[color=blue]
            > Fredrik,
            > ...I tried using your code...
            >
            > static long *get_long_array (PyObject *data, int *data_size) {
            > int i, size;
            > long* out;
            > PyObject* seq;
            >
            > seq = PySequence_Fast (data, "expected a sequence");
            > if (!seq)
            > return NULL;
            >
            > size = PySequence_Size (seq);
            > if (size < 0)
            > return NULL;
            >
            > if (data_size)
            > *data_size = size;
            >
            > out = (long*) PyMem_Malloc(si ze * sizeof(long));
            > if (!out) {
            > Py_DECREF(seq);
            > PyErr_NoMemory( );
            > return NULL;
            > }
            >
            > for (i = 0; i < size; i++)
            > out[i] = PyInt_AsLong(Py Sequence_Fast_G ET_ITEM(seq, i));
            >
            > Py_DECREF(seq);
            >
            > if (PyErr_Occurred ()) {
            > PyMem_Free(out) ;
            > out = NULL;
            > }
            >
            > return out;
            >
            > }
            >
            >
            > and I get this error..
            >
            > C:\project\myap p.c(549) : error C2040: 'get_long_array ' : 'long
            > *(struct _object *,int *)' differs in levels of indirection from 'int
            > ()'
            >
            > any idea?
            >
            > Fredrik Lundh wrote:[color=green]
            > > Jeremy Moles wrote:
            > >[color=darkred]
            > > > Probably what you want to do though is just keep the tuple as is and
            > > > iterate over it using the PySequence_* protocol:
            > > >
            > > > http://docs.python.org/api/sequence.html[/color]
            > >
            > > I did post a complete and tested example a few days ago, which contained
            > > code that showed how to do this. a complete waste of time, of course.
            > >
            > > </F>[/color][/color]

            Comment

            • Fredrik Lundh

              #7
              Re: Pass a tuple (or list) to a C wrapper function

              Java and Swing wrote:
              [color=blue]
              > and I get this error..
              >
              > C:\project\myap p.c(549) : error C2040: 'get_long_array ' : 'long
              > *(struct _object *,int *)' differs in levels of indirection from 'int
              > ()'[/color]

              so what's on line 549 in myapp.c?

              what other warnings did you get from the compiler?

              do you have other things named "get_long_array " in your program ?

              (I'm really beginning to think that you should take a break and learn
              a little more C before continuing on this task; C isn't that hard, but
              it's not really a language you can use to write programs by trial and
              error...)

              </F>



              Comment

              • Fredrik Lundh

                #8
                Re: Pass a tuple (or list) to a C wrapper function

                "Java and Swing" wrote:
                [color=blue]
                > anyhow, for receiving an object from python..is it
                >
                > ok = PyArg_ParseTupl e(args, "sO", &x, &y);
                >
                > ...is it "sO" or "s0" ....is it O (as in the letter) or 0 (as in the
                > number)? I would think "O" the letter..but it looks like a zero.[/color]

                eh? if you're not sure, what keeps you from cutting and pasting ?
                or comparing it visually with characters that you type in yourself ?

                (it's the letter O, for Object. the corresponding variable must be a
                pointer to a PyObject pointer. see the documentation for details:



                )

                </F>



                Comment

                Working...