Re: Ignoring default arguments in wrapper generation
Arnaud Diederen via Swig-user <[email protected]> Wed, 19 Feb 2025 10:42:56 +0100
| Newsgroups | gmane.comp.programming.swig |
|---|---|
| Message-ID | <CABSUo4O+m9YssB+gAn5gJRwirQRrOtCbdUMAA3Z1HHdYyFTi6w@mail.gmail.com> |
--===============1046334784775148376==
Content-Type: multipart/alternative; boundary="000000000000a73509062e7b95a1"
--000000000000a73509062e7b95a1
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Hi again,
I just noticed that my simplified example below, doesn't properly
illustrate the problem.
Indeed, the following:
```
int something(int foo, int bar=3D10);
```
_will_ be turned into a Python wrapper with proper type annotations &
default value, and that is because `10` is "known" to SWiG.
Let's take the following example instead:
```
int something(int foo, int bar=3DSOME_DEFINE_COMING_FROM_ANOTHER_HEADER);
```
the problem arises because of `SOME_DEFINE_COMING_FROM_ANOTHER_HEADER`.
That other header, will be converted to another Python module, so I cannot
just `%include` it (it'll duplicate functionality in both the current, and
that other module.)
I tried `%import`'ing it, but no luck.
If you happen to have suggestions, I'm all ears!
Best regards,
A.
On Wed, Feb 19, 2025 at 9:49=E2=80=AFAM Arnaud Diederen <[email protected]=
m> wrote:
> Hi,
>
> we have a relatively complex system that builds Python bindings on top of
> a SDK, and SWiG plays an important role early in the chain.
>
> An issue that has arisen, is the fact that from the following declaration=
:
> ```
> int something(int foo, int bar=3D10);
> ```
>
> SWiG will generate a Python function with the following signature:
> ```
> def something(*args):
> return _module.something(*args)
> ```
> =E2=80=A6and leave it up to `_module` to perform the dispatch between
> `_wrap_something__SWIG_0` and `_wrap_something__SWIG_1` (or
> `_wrap_something` if `compactdefaultargs` is used.)
>
> However, this is not really playing nice with the rest of our bindings
> "build system", because of the presence of `*args`.
>
> Something that would work much better for me, is if I could tell SWiG
> (ideally w/o `%ignore`'ing that function and then re-defining it myself),
> to simply "drop" the default value.
> In other words, I'd love it if I could tell SWiG to consider that
> declaration as if it were:
>
> ```
> int something(int foo, int bar);
> ```
> =E2=80=A6and our bindings build system would take it up from here.
>
> I realize this is perhaps not the canonical use-case for SWiG, but is
> there any chance I can achieve that, in a hopefully rather elegant manner=
?
>
> Note: `compactdefaultargs` does not work in this case, because while the
> default args values are assigned in the generated CPP wrapper code
> (great!), the Python wrapper itself still has only `*args` (ouch.)
>
> Any pointer welcome!
>
> Best regards,
> A.
>
>
>
> --
>
> Arnaud Diederen
>
> Head of Engineering
>
> [email protected] <[email protected]>
>
> +32 472 940 315
>
>
>
--=20
Arnaud Diederen
Head of Engineering
[email protected] <[email protected]>
+32 472 940 315
--000000000000a73509062e7b95a1
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div><br></div><div>Hi again,</div><div><br></div><div>I j=
ust noticed that my simplified example below, doesn't properly illustra=
te the problem.</div><div>Indeed, the following:</div><div><div>```</div><d=
iv>int something(int foo, int bar=3D10);</div><div>```</div><div>_will_ be =
turned into a Python wrapper with proper type annotations & default val=
ue, and that is because `10` is "known" to SWiG.</div><div><br></=
div><div>Let's take the following example instead:</div><div>```</div><=
div>int something(int foo, int bar=3DSOME_DEFINE_COMING_FROM_ANOTHER_HEADER=
);<br></div><div>```<br></div><div>the problem arises because of `SOME_DEFI=
NE_COMING_FROM_ANOTHER_HEADER`.</div><div><br></div><div>That other header,=
will be converted to another Python module, so I cannot just `%include` it=
(it'll duplicate functionality in both the current, and that other mod=
ule.)</div><div>I tried `%import`'ing it, but no luck.</div><div><br></=
div><div>If you happen to have suggestions, I'm all ears!</div><div><br=
></div><div>Best regards,</div><div>=C2=A0=C2=A0=C2=A0 A.<br></div><div><br=
></div><div><br></div></div></div><br><div class=3D"gmail_quote gmail_quote=
_container"><div dir=3D"ltr" class=3D"gmail_attr">On Wed, Feb 19, 2025 at 9=
:49=E2=80=AFAM Arnaud Diederen <<a href=3D"mailto:[email protected]">a=
[email protected]</a>> wrote:<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 dir=3D"ltr"><div><div>Hi,</div><div><br></div><div>w=
e have a relatively complex system=20
that builds Python bindings on top of a SDK, and SWiG plays an important
role early in the chain.</div><div><br></div><div>An issue that has arisen=
, is the fact that from the following declaration:<br>```</div><div>int som=
ething(int foo, int bar=3D10);</div><div>```</div><div><br></div><div>SWiG =
will generate a Python function with the following signature:</div><div>```=
</div><div>def something(*args):</div><div>=C2=A0 =C2=A0 =C2=A0return _modu=
le.something(*args)</div><div>```</div><div>=E2=80=A6and
leave it up to `_module` to perform the dispatch between=20
`_wrap_something__SWIG_0` and `_wrap_something__SWIG_1` (or=20
`_wrap_something` if `compactdefaultargs` is used.)</div><div><br></div><di=
v>However, this is not really playing nice with the rest of our bindings &q=
uot;build system", because of the presence of `*args`.</div><div><br><=
/div><div>Something
that would work much better for me, is if I could tell SWiG (ideally=20
w/o `%ignore`'ing that function and then re-defining it myself), to=20
simply "drop" the default value.</div><div>In other words, I'=
d love it if I could tell SWiG to consider that declaration as if=C2=A0 it =
were:</div><div><br></div><div>```</div><div>int something(int foo, int bar=
);</div><div>```</div><div>=E2=80=A6and our bindings build system would tak=
e it up from here.</div><div><br></div><div>I
realize this is perhaps not the canonical use-case for SWiG, but is=20
there any chance I can achieve that, in a hopefully rather elegant=20
manner?</div><div><br></div><div>Note: `compactdefaultargs` does not=20
work in this case, because while the default args values are assigned in
the generated CPP wrapper code (great!), the Python wrapper itself=20
still has only `*args` (ouch.)</div><div><br></div><div>Any pointer welcome=
!</div><div><br></div><div>Best regards,</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=
=A0A.</div><div></div><div><br><br></div></div><br><span class=3D"gmail_sig=
nature_prefix">-- </span><br><div dir=3D"ltr" class=3D"gmail_signature"><di=
v dir=3D"ltr"><span><p dir=3D"ltr" style=3D"line-height:1.38;margin-top:0pt=
;margin-bottom:0pt"><span style=3D"font-size:11pt;font-family:Arial,sans-se=
rif;color:rgb(0,0,0);background-color:transparent;font-weight:700;vertical-=
align:baseline">Arnaud Diederen</span></p><p dir=3D"ltr" style=3D"line-heig=
ht:1.38;margin-top:0pt;margin-bottom:0pt"><span style=3D"font-size:11pt;fon=
t-family:Arial,sans-serif;color:rgb(0,0,0);background-color:transparent;ver=
tical-align:baseline">Head of Engineering</span></p><p dir=3D"ltr" style=3D=
"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><a href=3D"mailto:steph=
[email protected]" target=3D"_blank"><span style=3D"font-size:11pt;font-fami=
ly:Arial,sans-serif;color:rgb(17,85,204);background-color:transparent;verti=
cal-align:baseline">[email protected]</span></a></p><p dir=3D"ltr" style=
=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style=3D"font-=
size:11pt;font-family:Arial,sans-serif;color:rgb(0,0,0);background-color:tr=
ansparent;vertical-align:baseline">+32 472 940 315</span></p><br><p dir=3D"=
ltr" style=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span styl=
e=3D"font-size:11pt;font-family:Arial,sans-serif;color:rgb(0,0,0);backgroun=
d-color:transparent;vertical-align:baseline"><span style=3D"border:medium;d=
isplay:inline-block;overflow:hidden;width:184px;height:49px"><img src=3D"ht=
tps://lh7-qw.googleusercontent.com/docsz/AD_4nXfzrRvtusEVQJYD8PML22x2ERSV2I=
tmaIzerZffxCjn4QggrPJhJ5b66qTEghBg621as8N536556Ajm_R3z-fyZ0MLJ0JEmp9g7wksw1=
zhrzsUBleawmAvTC4FULARENXG1pq0v2uVaNDcNlFceRAAa2Q?key=3DjodrV0E24pQyx0W124G=
eOQ" width=3D"184" height=3D"49" style=3D"margin-left: 0px; margin-top: 0px=
;"></span></span></p><div><span style=3D"font-size:11pt;font-family:Arial,s=
ans-serif;color:rgb(0,0,0);background-color:transparent;vertical-align:base=
line"><span style=3D"border:medium;display:inline-block;overflow:hidden;wid=
th:184px;height:49px"><br></span></span></div></span></div></div></div>
</blockquote></div><div><br clear=3D"all"></div><br><span class=3D"gmail_si=
gnature_prefix">-- </span><br><div dir=3D"ltr" class=3D"gmail_signature"><d=
iv dir=3D"ltr"><span><p dir=3D"ltr" style=3D"line-height:1.38;margin-top:0p=
t;margin-bottom:0pt"><span style=3D"font-size:11pt;font-family:Arial,sans-s=
erif;color:rgb(0,0,0);background-color:transparent;font-weight:700;vertical=
-align:baseline">Arnaud Diederen</span></p><p dir=3D"ltr" style=3D"line-hei=
ght:1.38;margin-top:0pt;margin-bottom:0pt"><span style=3D"font-size:11pt;fo=
nt-family:Arial,sans-serif;color:rgb(0,0,0);background-color:transparent;ve=
rtical-align:baseline">Head of Engineering</span></p><p dir=3D"ltr" style=
=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><a href=3D"mailto:st=
[email protected]" target=3D"_blank"><span style=3D"font-size:11pt;font-f=
amily:Arial,sans-serif;color:rgb(17,85,204);background-color:transparent;ve=
rtical-align:baseline">[email protected]</span></a></p><p dir=3D"ltr" sty=
le=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style=3D"fon=
t-size:11pt;font-family:Arial,sans-serif;color:rgb(0,0,0);background-color:=
transparent;vertical-align:baseline">+32 472 940 315</span></p><br><p dir=
=3D"ltr" style=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span =
style=3D"font-size:11pt;font-family:Arial,sans-serif;color:rgb(0,0,0);backg=
round-color:transparent;vertical-align:baseline"><span style=3D"border:medi=
um;display:inline-block;overflow:hidden;width:184px;height:49px"><img src=
=3D"https://lh7-qw.googleusercontent.com/docsz/AD_4nXfzrRvtusEVQJYD8PML22x2=
ERSV2ItmaIzerZffxCjn4QggrPJhJ5b66qTEghBg621as8N536556Ajm_R3z-fyZ0MLJ0JEmp9g=
7wksw1zhrzsUBleawmAvTC4FULARENXG1pq0v2uVaNDcNlFceRAAa2Q?key=3DjodrV0E24pQyx=
0W124GeOQ" width=3D"184" height=3D"49" style=3D"margin-left: 0px; margin-to=
p: 0px;"></span></span></p><div><span style=3D"font-size:11pt;font-family:A=
rial,sans-serif;color:rgb(0,0,0);background-color:transparent;vertical-alig=
n:baseline"><span style=3D"border:medium;display:inline-block;overflow:hidd=
en;width:184px;height:49px"><br></span></span></div></span></div></div>
--000000000000a73509062e7b95a1--
--===============1046334784775148376==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
--===============1046334784775148376==
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
--===============1046334784775148376==--