Re: PyList_New coredumps if called from inline function

Alexander Moibenko <[email protected]> Mon, 10 Nov 2025 12:25:42 +0300
Newsgroups gmane.comp.programming.swig
Message-ID <CALqOg-KUKP2vftW_g=GbAv2N3XnEcV1WE7LF7gTkm2dc3BpeYg@mail.gmail.com>
--===============2749272928294687243==
Content-Type: multipart/alternative; boundary="0000000000003238e406433a1efd"

--0000000000003238e406433a1efd
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Looks as I found the source of the problem.
I run swig as
swig -shadow -python -py3 -threads test_PyList_New.i
and get warning:
132 | Py_DEPRECATED(3.9) PyAPI_FUNC(void) PyEval_InitThreads(void);

when build .so module
I did not pay attention to this wagning, but it appears, that this is the
source of the problem.

If I run swig as
swig -shadow -python -py3 test_PyList_New.i
the call test_PyList_New() works.

Alexander

On Thu, Nov 6, 2025 at 5:41=E2=80=AFPM Alexander Moibenko <[email protected]=
om>
wrote:

> Hello,
> call of PyList_New causes coredump if called from inline function.
> Environment:
> OS: Ubuntu, or Alma Linux 9 (give same result)
> swig:
> SWIG Version 4.0.2
> Compiled with g++ [x86_64-pc-linux-gnu]
> Configured options: +pcre
>
> python: Python 3.12.7
>
> Code:
> swig:
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> %module test_PyList_New
>
> %inline %{
>   PyObject * test_PyList_New(const int buf_len)
>   {
>     PyObject * out_list;
>
>     printf("test_PyList_New requested buffer length %d\n", buf_len);
>     out_list =3D PyList_New(buf_len);
>     return NULL;
>   }
>     %}
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> Testing:
> python
> Python 3.12.7 (main, Oct 20 2025, 13:39:44) [GCC 11.4.0] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import test_PyList_New
> >>> test_PyList_New.test_PyList_New(20)
> test_PyList_New requested buffer length 20
> Segmentation fault (core dumped)
>
> This worked with earlier swig versions, at least 2.XX
> Please help.
> Thanks a lot in advance.
> Alexander
>
>

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

<div dir=3D"ltr"><div>Looks as I found the source of the problem.</div><div=
>I run swig as=C2=A0</div><div>swig -shadow -python -py3 -threads test_PyLi=
st_New.i</div><div>and get warning:</div><div>132 | Py_DEPRECATED(3.9) PyAP=
I_FUNC(void) PyEval_InitThreads(void);</div><div><br></div><div>when build =
.so module</div><div>I did not pay attention to this wagning, but it appear=
s, that this is the source of the problem.</div><div><br></div><div>If I ru=
n swig as=C2=A0</div><div>swig -shadow -python -py3 test_PyList_New.i</div>=
<div>the call=C2=A0test_PyList_New() works.</div><div><br></div><div>Alexan=
der</div></div><br><div class=3D"gmail_quote gmail_quote_container"><div di=
r=3D"ltr" class=3D"gmail_attr">On Thu, Nov 6, 2025 at 5:41=E2=80=AFPM Alexa=
nder Moibenko &lt;<a href=3D"mailto:[email protected]">[email protected]<=
/a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0=
px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><=
div dir=3D"ltr"><div>Hello,</div><div>call of=C2=A0PyList_New causes coredu=
mp=C2=A0if called from inline function.</div><div>Environment:</div><div>OS=
: Ubuntu, or Alma Linux=C2=A09 (give same result)</div><div>swig:</div><div=
>SWIG Version 4.0.2<br>Compiled with g++ [x86_64-pc-linux-gnu]<br>Configure=
d options: +pcre</div><div><br></div><div>python:=C2=A0Python 3.12.7</div><=
div><br></div><div>Code:</div><div>swig:</div><div>=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D</div><div>%module test_PyList_New<br><br>%inline %{<br>=C2=A0 =
PyObject * test_PyList_New(const int buf_len)<br>=C2=A0 {<br>=C2=A0 =C2=A0 =
PyObject * out_list;<br><br>=C2=A0 =C2=A0 printf(&quot;test_PyList_New requ=
ested buffer length %d\n&quot;, buf_len);<br>=C2=A0 =C2=A0 out_list =3D PyL=
ist_New(buf_len);<br>=C2=A0 =C2=A0 return NULL;<br>=C2=A0 }<br>=C2=A0 =C2=
=A0 %}<br>=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D</div><div>Testing:</div=
><div>python<br>Python 3.12.7 (main, Oct 20 2025, 13:39:44) [GCC 11.4.0] on=
 linux<br>Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot;=
 or &quot;license&quot; for more information.<br>&gt;&gt;&gt; import test_P=
yList_New<br>&gt;&gt;&gt; test_PyList_New.test_PyList_New(20)<br>test_PyLis=
t_New requested buffer length 20<br>Segmentation fault (core dumped)<br><br=
></div><div>This worked with earlier swig versions, at least 2.XX=C2=A0</di=
v><div>Please help.</div><div>Thanks a lot in advance.</div><div>Alexander<=
/div><div><br></div></div>
</blockquote></div>

--0000000000003238e406433a1efd--


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


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

--===============2749272928294687243==--