Re: Python bindings and large std::string value produces char * swig object

William S Fulton <[email protected]> Tue, 28 Jan 2025 22:17:37 +0000
Newsgroups gmane.comp.programming.swig
Message-ID <CANGqftDtP5=A1Fk66q5pLT02t2P9qwOXavh1QdCyCcD3DtMKtA@mail.gmail.com>
--===============3319083185124179086==
Content-Type: multipart/alternative; boundary="000000000000a0c505062ccb917c"

--000000000000a0c505062ccb917c
Content-Type: text/plain; charset="UTF-8"

I suppose with extremely large strings, you may have an out of memory
situation and/or memory corruption in either your own code or the SWIG
wrapped code resulting in undefined behaviour. Alas, you'll need to debug
it like any other C++ code to find the problem.

William

On Mon, 27 Jan 2025 at 22:13, <[email protected]> wrote:

>
>
> Dear Swig Community,
>
>
>
> I am using swig 4.3.0 and Python 3.9.5 on an x64 Windows 11 platform.  I
> have a third-party Python database module compiled with Visual Studio C++
> 2017 that uses swig bindings, originally designed for Python 2.   One class
> that we are using has a method that returns strings to Python.  For example:
>
>
>
> > str_val = v.asString()   # v is an instance of the class in question
>
>
>
> If v is the swig object, str_val = v.asString() should store the string to
> variable str_val.
>
>
>
> Most of the time this works flawlessly.  However, for very large results,
> str_val does not receive the value of the string, but rather another swig
> proxy object for a pointer to a character string:
>
>
>
> > str_val = v.asString()
>
> > str_val
>
>     <Swig Object of type 'char *' at 0x000001951F1F5990>
>
>
>
> The underlying C++ library declares the method as returning std::string
> and we have included std_string.i in our interface.  I did modify some of
> the other templates in the library to use the Python library Unicode
> interface, but I do not think that this should be relevant for a
> std::string return.
>
>
>
> An unsolved 2018 post on stackoverflow reported what seems to report the
> same issue (
> https://stackoverflow.com/questions/50056859/c-string-to-python-limitation-using-swig,
> they note the error at strings longer than 2 GB which is probably my case),
> does anyone have suggestions?  I also saw this behavior with swig 4.1.1.
>
>
>
> Thank you,
>
> Marie
>
>
>
>
>
>
> _______________________________________________
> Swig-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/swig-user
>

--000000000000a0c505062ccb917c
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>I suppose with extremely large strings, you may have =
an out of memory situation and/or memory corruption in either your own code=
 or the SWIG wrapped code resulting in undefined behaviour. Alas, you&#39;l=
l need to debug it like any other C++ code to find the problem.<br></div><d=
iv><br></div><div>William<br></div></div><br><div class=3D"gmail_quote gmai=
l_quote_container"><div dir=3D"ltr" class=3D"gmail_attr">On Mon, 27 Jan 202=
5 at 22:13, &lt;<a href=3D"mailto:[email protected]">[email protected]</a>&gt; wr=
ote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px=
 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class=
=3D"msg4115349922843334092"><div style=3D"overflow-wrap: break-word;" lang=
=3D"EN-US"><div class=3D"m_4115349922843334092WordSection1"><p class=3D"Mso=
Normal"><u></u>=C2=A0<u></u></p><p class=3D"MsoNormal">Dear Swig Community,=
<u></u><u></u></p><p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p><p class=
=3D"MsoNormal">I am using swig 4.3.0 and Python 3.9.5 on an x64 Windows 11 =
platform.=C2=A0 I have a third-party Python database module compiled with V=
isual Studio C++ 2017 that uses swig bindings, originally designed for Pyth=
on 2.=C2=A0 =C2=A0One class that we are using has a method that returns str=
ings to Python.=C2=A0 For example:<u></u><u></u></p><p class=3D"MsoNormal">=
<u></u>=C2=A0<u></u></p><p class=3D"MsoNormal">&gt; str_val =3D v.asString(=
)=C2=A0=C2=A0 # v is an instance of the class in question <u></u><u></u></p=
><p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p><p class=3D"MsoNormal">If v=
 is the swig object, str_val =3D v.asString() should store the string to va=
riable str_val.<u></u><u></u></p><p class=3D"MsoNormal"><u></u>=C2=A0<u></u=
></p><p class=3D"MsoNormal">Most of the time this works flawlessly.=C2=A0 H=
owever, for very large results, str_val does not receive the value of the s=
tring, but rather another swig proxy object for a pointer to a character st=
ring:<u></u><u></u></p><p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p><p cl=
ass=3D"MsoNormal">&gt; str_val =3D v.asString()<u></u><u></u></p><p class=
=3D"MsoNormal">&gt; str_val<u></u><u></u></p><p class=3D"MsoNormal">=C2=A0=
=C2=A0 =C2=A0&lt;Swig Object of type &#39;char *&#39; at 0x000001951F1F5990=
&gt;<u></u><u></u></p><p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p><p cla=
ss=3D"MsoNormal">The underlying C++ library declares the method as returnin=
g std::string and we have included std_string.i in our interface.=C2=A0 I d=
id modify some of the other templates in the library to use the Python libr=
ary Unicode interface, but I do not think that this should be relevant for =
a std::string return. <u></u><u></u></p><p class=3D"MsoNormal"><u></u>=C2=
=A0<u></u></p><p class=3D"MsoNormal">An unsolved 2018 post on stackoverflow=
 reported what seems to report the same issue (<a href=3D"https://stackover=
flow.com/questions/50056859/c-string-to-python-limitation-using-swig" targe=
t=3D"_blank">https://stackoverflow.com/questions/50056859/c-string-to-pytho=
n-limitation-using-swig</a>, they note the error at strings longer than 2 G=
B which is probably my case), does anyone have suggestions?=C2=A0 I also sa=
w this behavior with swig 4.1.1.<u></u><u></u></p><p class=3D"MsoNormal"><u=
></u>=C2=A0<u></u></p><p class=3D"MsoNormal">Thank you,<u></u><u></u></p><p=
 class=3D"MsoNormal">Marie<u></u><u></u></p><p class=3D"MsoNormal"><u></u>=
=C2=A0<u></u></p><p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p><p class=3D=
"MsoNormal"><u></u>=C2=A0<u></u></p></div></div>___________________________=
____________________<br>
Swig-user mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">Swig-u=
[email protected]</a><br>
<a href=3D"https://lists.sourceforge.net/lists/listinfo/swig-user" rel=3D"n=
oreferrer" target=3D"_blank">https://lists.sourceforge.net/lists/listinfo/s=
wig-user</a><br>
</div></blockquote></div>

--000000000000a0c505062ccb917c--


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


--===============3319083185124179086==
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

--===============3319083185124179086==--