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

<[email protected]> Mon, 27 Jan 2025 13:06:58 -0800
Newsgroups gmane.comp.programming.swig
Message-ID <[email protected]>
This is a multipart message in MIME format.

--===============0220029577633131751==
Content-Type: multipart/alternative;
	boundary="----=_NextPart_000_0397_01DB70BC.59A6AE60"
Content-Language: en-us

This is a multipart message in MIME format.

------=_NextPart_000_0397_01DB70BC.59A6AE60
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: 7bit

 

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

 

 

 


------=_NextPart_000_0397_01DB70BC.59A6AE60
Content-Type: text/html;
	charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<html xmlns:v=3D"urn:schemas-microsoft-com:vml" =
xmlns:o=3D"urn:schemas-microsoft-com:office:office" =
xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" =
xmlns=3D"http://www.w3.org/TR/REC-html40"><head><meta =
http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dus-ascii"><meta name=3DGenerator content=3D"Microsoft Word 15 =
(filtered medium)"><style><!--
/* Font Definitions */
@font-face
	{font-family:Wingdings;
	panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
	{font-family:"Cambria Math";
	panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
	{font-family:Aptos;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0in;
	font-size:11.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;
	font-size:11.0pt;}
@page WordSection1
	{size:8.5in 11.0in;
	margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
	{page:WordSection1;}
/* List Definitions */
@list l0
	{mso-list-id:1872298802;
	mso-list-type:hybrid;
	mso-list-template-ids:34628000 1458084068 67698691 67698693 67698689 =
67698691 67698693 67698689 67698691 67698693;}
@list l0:level1
	{mso-level-start-at:0;
	mso-level-number-format:bullet;
	mso-level-text:\F0D8;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;
	font-family:Wingdings;
	mso-fareast-font-family:Aptos;
	mso-bidi-font-family:"Times New Roman";}
@list l0:level2
	{mso-level-number-format:bullet;
	mso-level-text:o;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;
	font-family:"Courier New";}
@list l0:level3
	{mso-level-number-format:bullet;
	mso-level-text:\F0A7;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;
	font-family:Wingdings;}
@list l0:level4
	{mso-level-number-format:bullet;
	mso-level-text:\F0B7;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;
	font-family:Symbol;}
@list l0:level5
	{mso-level-number-format:bullet;
	mso-level-text:o;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;
	font-family:"Courier New";}
@list l0:level6
	{mso-level-number-format:bullet;
	mso-level-text:\F0A7;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;
	font-family:Wingdings;}
@list l0:level7
	{mso-level-number-format:bullet;
	mso-level-text:\F0B7;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;
	font-family:Symbol;}
@list l0:level8
	{mso-level-number-format:bullet;
	mso-level-text:o;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;
	font-family:"Courier New";}
@list l0:level9
	{mso-level-number-format:bullet;
	mso-level-text:\F0A7;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;
	font-family:Wingdings;}
ol
	{margin-bottom:0in;}
ul
	{margin-bottom:0in;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext=3D"edit">
<o:idmap v:ext=3D"edit" data=3D"1" />
</o:shapelayout></xml><![endif]--></head><body lang=3DEN-US =
link=3D"#467886" vlink=3D"#96607D" style=3D'word-wrap:break-word'><div =
class=3DWordSection1><p class=3DMsoNormal><o:p>&nbsp;</o:p></p><p =
class=3DMsoNormal>Dear Swig Community,<o:p></o:p></p><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal>I am using =
swig 4.3.0 and Python 3.9.5 on an x64 Windows 11 platform.&nbsp; I have =
a third-party Python database module compiled with Visual Studio C++ =
2017 that uses swig bindings, originally designed for Python 2.&nbsp; =
&nbsp;One class that we are using has a method that returns strings to =
Python.&nbsp; For example:<o:p></o:p></p><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal>&gt; str_val =
=3D v.asString()&nbsp;&nbsp; # v is an instance of the class in question =
<o:p></o:p></p><p class=3DMsoNormal><o:p>&nbsp;</o:p></p><p =
class=3DMsoNormal>If v is the swig object, str_val =3D v.asString() =
should store the string to variable str_val.<o:p></o:p></p><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal>Most of the =
time this works flawlessly.&nbsp; 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:<o:p></o:p></p><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal>&gt; str_val =
=3D v.asString()<o:p></o:p></p><p class=3DMsoNormal>&gt; =
str_val<o:p></o:p></p><p class=3DMsoNormal>&nbsp;&nbsp; &nbsp;&lt;Swig =
Object of type 'char *' at 0x000001951F1F5990&gt;<o:p></o:p></p><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal>The =
underlying C++ library declares the method as returning std::string and =
we have included std_string.i in our interface.&nbsp; 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. <o:p></o:p></p><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal>An unsolved =
2018 post on stackoverflow reported what seems to report the same issue =
(https://stackoverflow.com/questions/50056859/c-string-to-python-limitati=
on-using-swig, they note the error at strings longer than 2 GB which is =
probably my case), does anyone have suggestions?&nbsp; I also saw this =
behavior with swig 4.1.1.<o:p></o:p></p><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal>Thank =
you,<o:p></o:p></p><p class=3DMsoNormal>Marie<o:p></o:p></p><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p></div></body></html>
------=_NextPart_000_0397_01DB70BC.59A6AE60--



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


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

--===============0220029577633131751==--