operator+= in python

"Langer, Stephen A. \(Fed\) via Swig-user" <[email protected]> Fri, 13 Jun 2025 20:18:41 +0000
Newsgroups gmane.comp.programming.swig
Message-ID <DS0PR09MB10532B3143B3E119711738AE7E677A@DS0PR09MB10532.namprd09.prod.outlook.com>
--===============8695298893424298288==
Content-Language: en-US
Content-Type: multipart/alternative;
	boundary="_000_DS0PR09MB10532B3143B3E119711738AE7E677ADS0PR09MB10532na_"

--_000_DS0PR09MB10532B3143B3E119711738AE7E677ADS0PR09MB10532na_
Content-Type: text/plain; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

Hi --

What=92s the correct way to use SWIG to create a Python version of C++ comp=
ound assignment operator, like operator+=3D ?    The simple thing isn=92t d=
oing what I expect.
For an object of a Python class that defines __iadd__,
    pyobject +=3D other_pyobject
doesn=92t change the identity of pyobject.  But for a C++ object that defin=
es operator+=3D, SWIG creates an __iadd__ method that returns a new Python =
object.
So
      swigobject +=3D other_swigobject
changes the identity of swigobject, although it still refers to the same C+=
+ object.  When the redundant Python object is deleted, the C++ destructor =
is called an extra time, which is bad.

The swig file just declares a simple class that defines MyClass& operator+=
=3D(const MyClass&).  The generated __iadd__ wrapper function ends with
      resultobj =3D SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_=
MyClass, SWIG_POINTER_OWN |  0 );
      return resultobj;
Changing SWIG_POINTER_OWN|0 to 0 fixes the problem, as far as my simple tes=
ts show.  I don=92t know if it introduces other problems.

I=92ve tried SWIG 4.1 to 4.3.1, and they all behave the same.

Thanks.
 -- Steve



--_000_DS0PR09MB10532B3143B3E119711738AE7E677ADS0PR09MB10532na_
Content-Type: text/html; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

<html xmlns:o=3D"urn:schemas-microsoft-com:office:office" xmlns:w=3D"urn:sc=
hemas-microsoft-com:office:word" xmlns:m=3D"http://schemas.microsoft.com/of=
fice/2004/12/omml" xmlns=3D"http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3DWindows-1=
252">
<meta name=3D"Generator" content=3D"Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
	{font-family:"Cambria Math";
	panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
	{font-family:Aptos;
	panose-1:2 11 0 4 2 2 2 2 2 4;}
@font-face
	{font-family:"Times New Roman \(Body CS\)";
	panose-1:2 11 6 4 2 2 2 2 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0in;
	font-size:12.0pt;
	font-family:"Aptos",sans-serif;
	mso-ligatures:standardcontextual;}
span.EmailStyle17
	{mso-style-type:personal-compose;
	font-family:"Aptos",sans-serif;
	color:windowtext;}
.MsoChpDefault
	{mso-style-type:export-only;}
@page WordSection1
	{size:8.5in 11.0in;
	margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
	{page:WordSection1;}
--></style>
</head>
<body lang=3D"EN-US" link=3D"#467886" vlink=3D"#96607D" style=3D"word-wrap:=
break-word">
<div class=3D"WordSection1">
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt">Hi --<o:p></o:p></s=
pan></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt"><o:p>&nbsp;</o:p></=
span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt">What=92s the correc=
t way to use SWIG to create a Python version of C++ compound assignment ope=
rator, like operator+=3D ?&nbsp;&nbsp;&nbsp; The simple thing isn=92t doing=
 what I expect.<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt"><o:p></o:p></span><=
/p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt">For an object of a =
Python class that defines __iadd__,<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt">&nbsp;&nbsp;&nbsp;&=
nbsp;pyobject +=3D other_pyobject<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt">doesn=92t change th=
e identity of pyobject.&nbsp; But for a C++ object that defines operator+=
=3D, SWIG creates an __iadd__ method that returns a new Python object.&nbsp=
;&nbsp;
<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt">So<o:p></o:p></span=
></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt">&nbsp; &nbsp;&nbsp;=
&nbsp;&nbsp;swigobject +=3D other_swigobject<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt">changes the identit=
y of swigobject, although it still refers to the same C++ object.&nbsp; Whe=
n the redundant Python object is deleted, the C++ destructor is called an e=
xtra time, which is bad.<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt"><o:p>&nbsp;</o:p></=
span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt">The swig file just =
declares a simple class that defines MyClass&amp; operator+=3D(const MyClas=
s&amp;).&nbsp; The generated __iadd__ wrapper function ends with
<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:10.5pt">&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;resultobj =3D SWIG_NewPointerObj(SWIG_as_voidptr(result), =
SWIGTYPE_p_MyClass, SWIG_POINTER_OWN |&nbsp; 0 );<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:10.5pt">&nbsp;&nbsp; &nbsp;=
&nbsp;&nbsp;return resultobj;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt">Changing SWIG_POINT=
ER_OWN|0 to 0 fixes the problem, as far as my simple tests show.&nbsp; I do=
n=92t know if it introduces other problems.<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt"><o:p>&nbsp;</o:p></=
span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt">I=92ve tried SWIG 4=
.1 to 4.3.1, and they all behave the same.<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt"><o:p>&nbsp;</o:p></=
span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt">Thanks. <o:p></o:p>=
</span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt">&nbsp;-- Steve<o:p>=
</o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt"><o:p>&nbsp;</o:p></=
span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:10.5pt"><o:p>&nbsp;</o:p></=
span></p>
</div>
</body>
</html>

--_000_DS0PR09MB10532B3143B3E119711738AE7E677ADS0PR09MB10532na_--


--===============8695298893424298288==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


--===============8695298893424298288==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user

--===============8695298893424298288==--