Swig and pointers

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

    #1

    Swig and pointers

    I've tried sending a email to the swig mailing list 3 times...but it
    never seems to get it...not sure what is going on, still looking into
    it. Until then...

    I now have a C function such as...

    int doIt(char *a, MY_DIGIT **digit) {
    ...
    }

    so I am trying to figure out how to pass in that "digit" pointer.

    My SWIG interface file looks like...

    extern int doIt(char *a, MY_DIGIT **digit);
    %include cpointer.i
    %pointer_functi ons(MY_DIGIT, md_prt);
    %typemap(in) (char *a, MY_DIGIT **argv) {
    /* Check if is a list */
    if (PyList_Check($ input)) {
    int i;
    $1 = PyList_Size($in put);
    $2 = (MY_DIGIT **) malloc(($1+1)*s izeof(long *));
    for (i = 0; i < $1; i++) {
    PyObject *o = PyList_GetItem( $input,i);
    if (PyString_Check (o))
    $2[i] = PyString_AsStri ng(PyList_GetIt em($input,i));
    else {
    PyErr_SetString (PyExc_TypeErro r,"list must contain strings");
    free($2);
    return NULL;
    }
    }
    $2[i] = 0;
    } else {
    PyErr_SetString (PyExc_TypeErro r,"not a list");
    return NULL;
    }
    }

    %typemap(freear g) (char *a, MY_DIGIT **argv) {
    free((MY_DIGIT *) $2);
    }


    ...from Python I am trying
    [color=blue][color=green]
    >> ptr = []
    >> doIt("blah", ptr)[/color][/color]

    I thought this was the correct approach as described here:


    However, python comes back and says "TypeError: argument number 2: a
    'MY_DIGIT **' is expected, 'list([])' is received"

    ...any ideas? Thanks for all the help. The python community has been
    great.

  • Miki Tebeka

    #2
    Re: Swig and pointers

    Hello Java,
    [color=blue]
    > ...
    > extern int doIt(char *a, MY_DIGIT **digit);
    > %include cpointer.i
    > %pointer_functi ons(MY_DIGIT, md_prt);[/color]
    Don't you mean md_ptr?
    [color=blue]
    > %typemap(in) (char *a, MY_DIGIT **argv) {
    > /* Check if is a list */
    > if (PyList_Check($ input)) {
    > int i;
    > $1 = PyList_Size($in put);
    > $2 = (MY_DIGIT **) malloc(($1+1)*s izeof(long *));
    > for (i = 0; i < $1; i++) {
    > PyObject *o = PyList_GetItem( $input,i);
    > if (PyString_Check (o))
    > $2[i] = PyString_AsStri ng(PyList_GetIt em($input,i));
    > else {
    > PyErr_SetString (PyExc_TypeErro r,"list must contain strings");
    > free($2);
    > return NULL;
    > }
    > }
    > $2[i] = 0;
    > } else {
    > PyErr_SetString (PyExc_TypeErro r,"not a list");
    > return NULL;
    > }
    > }
    >
    > %typemap(freear g) (char *a, MY_DIGIT **argv) {
    > free((MY_DIGIT *) $2);
    > }
    >
    >
    > ..from Python I am trying
    > [color=green][color=darkred]
    > >> ptr = []
    > >> doIt("blah", ptr)[/color][/color]
    >
    > I thought this was the correct approach as described here:
    > http://www.swig.org/Doc1.3/SWIGDocum...ml#Python_nn59
    >
    > However, python comes back and says "TypeError: argument number 2: a
    > 'MY_DIGIT **' is expected, 'list([])' is received"
    >
    > ..any ideas? [/color]
    Didn't check it but from http://www.swig.org/Doc1.3/Library.html it looks
    like you need to do:
    ptr = new_md_prt()

    HTH.
    --
    ------------------------------------------------------------------------
    Miki Tebeka <mtebeka@qualco mm.com>

    The only difference between children and adults is the price of the toys

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (Cygwin)

    iD8DBQFDRPq78jA dENsUuJsRAiwfAJ 43iL7y8B1PNSgzu YVte+40UuDI5wCg q/4K
    vSRoo7AzIRZewYt 1SYxMuXU=
    =gLFG
    -----END PGP SIGNATURE-----

    Comment

    • Java and Swing

      #3
      Re: Swig and pointers

      im sorry, why would it be md_ptr? what is md_ptr?

      i tried..
      %include cpointer.i
      %pointer_functi ons(MY_DIGIT, digit_ptr)

      ptr = new_digit_ptr()
      doIt("a message", ptr)
      ....doesnt work..still needs a DIGIT **

      Comment

      • Java and Swing

        #4
        Re: Swig and pointers

        (reposting, i was just told how I should properly reply via
        groups.google.c om)

        im sorry, why would it be md_ptr? what is md_ptr?

        i tried..
        %include cpointer.i
        %pointer_functi ons(MY_DIGIT, digit_ptr)

        ptr = new_digit_ptr()
        doIt("a message", ptr)
        ....doesnt work..still needs a DIGIT **


        Miki Tebeka wrote:[color=blue]
        > Hello Java,
        >[color=green]
        > > ...
        > > extern int doIt(char *a, MY_DIGIT **digit);
        > > %include cpointer.i
        > > %pointer_functi ons(MY_DIGIT, md_prt);[/color]
        > Don't you mean md_ptr?
        >[color=green]
        > > %typemap(in) (char *a, MY_DIGIT **argv) {
        > > /* Check if is a list */
        > > if (PyList_Check($ input)) {
        > > int i;
        > > $1 = PyList_Size($in put);
        > > $2 = (MY_DIGIT **) malloc(($1+1)*s izeof(long *));
        > > for (i = 0; i < $1; i++) {
        > > PyObject *o = PyList_GetItem( $input,i);
        > > if (PyString_Check (o))
        > > $2[i] = PyString_AsStri ng(PyList_GetIt em($input,i));
        > > else {
        > > PyErr_SetString (PyExc_TypeErro r,"list must contain strings");
        > > free($2);
        > > return NULL;
        > > }
        > > }
        > > $2[i] = 0;
        > > } else {
        > > PyErr_SetString (PyExc_TypeErro r,"not a list");
        > > return NULL;
        > > }
        > > }
        > >
        > > %typemap(freear g) (char *a, MY_DIGIT **argv) {
        > > free((MY_DIGIT *) $2);
        > > }
        > >
        > >
        > > ..from Python I am trying
        > >[color=darkred]
        > > >> ptr = []
        > > >> doIt("blah", ptr)[/color]
        > >
        > > I thought this was the correct approach as described here:
        > > http://www.swig.org/Doc1.3/SWIGDocum...ml#Python_nn59
        > >
        > > However, python comes back and says "TypeError: argument number 2: a
        > > 'MY_DIGIT **' is expected, 'list([])' is received"
        > >
        > > ..any ideas?[/color]
        > Didn't check it but from http://www.swig.org/Doc1.3/Library.html it looks
        > like you need to do:
        > ptr = new_md_prt()
        >
        > HTH.
        > --
        > ------------------------------------------------------------------------
        > Miki Tebeka <mtebeka@qualco mm.com>
        > http://tebeka.bizhat.com
        > The only difference between children and adults is the price of the toys[/color]

        Comment

        Working...