Marcelo Matus | 9 May 19:29

Re: Making smart pointers a bit smarter

Anything that get prevents a seg fault is welcome :),

Maybe you can try to implement this as a "feature",
that could be enabled by default for smart pointers
but that cam also be disabled for testing and backward
compatibility.

Marcelo

Kenneth Duda wrote:

>Hi folks,
>
>SWIG  (at least swig -python) has nice support for wrapping C++ smart
>pointers.  One thing that I feel could be improved is the handling of
>NULL smart pointers.  When converting a NULL C++ raw pointer to
>python, SWIG (correctly, in my view) converts it to None.  However,
>SWIIG doesn't do anything similar for smart pointers --- it basically
>wraps the NULL smart pointer and returns it.  A NULL smart pointer
>wrapped this way is a booby trap --- any attempt to use it for
>anything causes a SEGV.
>
>More specifically, the default typemap results in the following code
>to return a smart pointer to python:
>
>  {
>      FooSmartptr * resultptr;
>      resultptr = new FooSmartptr((FooSmartptr &) result);
>      resultobj = SWIG_NewPointerObj((void *) resultptr,
>SWIGTYPE_p_FooSmartptr, 1);
>  }
>
>With the following typemap, the behavior can be improved (in my
>opinion) so that None is returned if the smart pointer is NULL:
>
>%typemap(out) FooSmartptr {
> if( $1.operator->() ) {
>    FooSmartptr * resultptr;
>    resultptr = new FooSmartptr( $1 );
>    $result = SWIG_NewPointerObj( resultptr, SWIGTYPE_p_FooSmartptr, 1 );
> } else {
>    Py_INCREF(Py_None);
>    $result = Py_None;
> }
>}
>
>"None" is much friendlier than a wrapped NULL smart pointer --- it's
>easy to test for "None", and if you try to use it as a pointer, you
>get a sensible Python exception (rather than a SEGV).
>
>All of this is fine --- the reason I'm writing is I would like to make
>this improved smart pointer wrapping the default behavior of SWIG.
>Before I spend the time to do so, I wanted to guage the interest level
>from the SWIG maintainers --- is the patch likely to be accepted
>(assuming I code it competently), or is it something you're not
>interested in (i.e., is there a good reason to not do this)?
>
>Thanks,
>  -Ken
>_______________________________________________
>Swig maillist  -  Swig <at> cs.uchicago.edu
>http://mailman.cs.uchicago.edu/mailman/listinfo/swig
>
>  
>

_______________________________________________
Swig maillist  -  Swig <at> cs.uchicago.edu
http://mailman.cs.uchicago.edu/mailman/listinfo/swig


Gmane