SWIG and char* newb questions :)

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

    SWIG and char* newb questions :)

    Hi i'm relatively new to Python and my C/C++ knowledge is near to
    None. Having said that I feel justified to ask stupid questions :)

    Ok now more seriously. I have question refering to char* used as
    function parameters to return values. I have read SWIG manual to find
    best way to overcome that, but there are many warnings about memory
    leaks and stuff, so I feel confused.

    Ok to put it more simply: how to safely define a variable in Python
    and have it modified by C/C++ function?
    Even better would be a way to make a tuple of return value and out
    parameters, but thats probably a lot more work.

    Any hint will be appreciated!
  • Heiko Wundram

    #2
    Re: SWIG and char* newb questions :)

    Am Dienstag, 29. Juli 2008 12:51:36 schrieb code_berzerker:
    Ok now more seriously. I have question refering to char* used as
    function parameters to return values. I have read SWIG manual to find
    best way to overcome that, but there are many warnings about memory
    leaks and stuff, so I feel confused.
    >
    Ok to put it more simply: how to safely define a variable in Python
    and have it modified by C/C++ function?
    At least for strings, this won't work. Python strings are immutable (and
    Python optimizes some things based on this knowledge), and as such you can
    pass a Python string(-object) into a C/C++ function and retrieve its value
    there as a const (!) char* using the PyString_*-API (I have no idea how this
    is encapsulated in SWIG), but cannot/should not modify it (a const_cast<is
    almost always a sign of bad programming, anyway).

    The only real choice you have is to have your wrapper return a new string
    object, created using one of the PyString_FromSt ring[AndSize] functions.
    Check the Python C-API documentation for more info on this.

    Anyway, on a different note, I personally have always found it simpler to not
    use SWIG to generate C extensions for Python, but to use the Python C-API
    directly.

    Hope this helps!

    --
    Heiko Wundram

    Comment

    • code_berzerker

      #3
      Re: SWIG and char* newb questions :)

      Ok I think I got it:


      PyObject* myFuncXXX(char* p_1, int p_2, char* p_3, int p_4)
      {
      int res;
      char _host[255] = "";
      int _port;
      res = funcXXX(p_1, p_2, p_3, p_4, _host, &_port);

      PyObject* res1 = PyInt_FromLong( res);
      PyObject* res2 = PyString_FromSt ringAndSize(_ho st, strlen(_host));
      PyObject* res3 = PyInt_FromLong( _port);

      PyObject* resTuple = PyTuple_New(3);

      PyTuple_SetItem (resTuple, 0, res1);
      PyTuple_SetItem (resTuple, 1, res2);
      PyTuple_SetItem (resTuple, 2, res3);

      return resTuple;
      }

      It seems to work when I put it into swig's "*.i" file.

      me proud of me.self :D

      Comment

      • Stefan Behnel

        #4
        Re: SWIG and char* newb questions :)

        code_berzerker wrote:
        Hi i'm relatively new to Python and my C/C++ knowledge is near to
        None. Having said that I feel justified to ask stupid questions :)
        Have you considered using Cython? It's almost Python, but it compiles to C
        code for a Python extension module and even lets you call C functions directly
        from your code.



        Stefan

        Comment

        Working...