Menu

#25 Python: better inplace arithmetic operator support

closed
nobody
None
5
2004-12-11
2004-04-08
No

The current wrapping of inplace operators for Python
produces surprising output to most users for the most
commonly used case. For example if a function with
this signature

Vector& operator += (Vector &v);

is wrapped and the function returns (*this) then the
following code will crash the Python interpreter.

>>> a,b = Vector(1,1,1), Vector(1,1,1)
>>> a += b

This is surprising behavior for a user wrapping their code.
This issue is discussed in detail in this thread:

http://mailman.cs.uchicago.edu/pipermail/swig/2004-April/009547.html

One suggested approach to fix this is to add something
like this:

%pythoncode %{
_old_iadd =3D __iadd__
def __iadd__(self,v):
self._old_iadd(v)
return self
%}

This works because when self._old_iadd is called a
VectorPtr is
returned for which thisown = 0, hence the object is not
deleted.

It would be great if this behavior were made the
default when an inplace operator that returns a
reference is wrapped by SWIG.

Thanks!
prabhu

Discussion

  • Marcelo Matus

    Marcelo Matus - 2004-12-11
    • status: open --> closed
     
  • Marcelo Matus

    Marcelo Matus - 2004-12-11

    Logged In: YES
    user_id=246059

    already included,

    thanks,

    marcelo

     

Log in to post a comment.