Re: Ignoring default arguments in wrapper generation
William S Fulton <[email protected]> Sun, 23 Feb 2025 17:20:19 +0000
| Newsgroups | gmane.comp.programming.swig |
|---|---|
| Message-ID | <CANGqftC3Y_G9CV0ZoXLgZb-3vJ3PLeGwppbM-yGJyUQOJ6TTHA@mail.gmail.com> |
--===============7170769211647679092== Content-Type: multipart/alternative; boundary="0000000000004a7b0b062ed2724e" --0000000000004a7b0b062ed2724e Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Arguably the following should work for you: %ignore something(int foo); int something(int foo, int bar=3DSOME_DEFINE_COMING_FROM_ANOTHER_HEADER); like it does for Java. It partially does the ignore as the C++ layer only generates code for taking two arguments, but the python layer defines: def something(*args): when it could generate def something(foo, bar): You could raise this as an issue in Github, but at least you have a workaround. William On Wed, 19 Feb 2025 at 10:11, Arnaud Diederen via Swig-user < [email protected]> wrote: > > ....aaaaaaand, > > On Wed, Feb 19, 2025 at 10:42=E2=80=AFAM Arnaud Diederen <arnaud@hex-rays= .com> > wrote: > >> [...]the problem arises because of >> `SOME_DEFINE_COMING_FROM_ANOTHER_HEADER`. >> > > Wrong again. > > The problem is that the define has syntax that SWiG dislikes: > > `#define SOME_DEFINE_COMING_FROM_ANOTHER_HEADER thread_id_t(-1)` > > However, that is pretty good news, as I have full control over this. > Looks like I can proceed with my rube-goldbergery! > Best regards, > A. > > >> >> 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 <arnaud@hex-rays= .com> >> 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 mann= er? >>> >>> Note: `compactdefaultargs` does not work in this case, because while th= e >>> 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 >>> >>> >>> >> >> -- >> >> Arnaud Diederen >> >> Head of Engineering >> >> [email protected] <[email protected]> >> >> +32 472 940 315 >> >> >> > > -- > > Arnaud Diederen > > Head of Engineering > > [email protected] <[email protected]> > > +32 472 940 315 > > > _______________________________________________ > Swig-user mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/swig-user > --0000000000004a7b0b062ed2724e Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div><br></div><div>Arguably the following should work for= you:</div><div><br></div><div>%ignore something(int foo);<br></div><div>in= t something(int foo, int bar=3DSOME_DEFINE_COMING_FROM_ANOTHER_HEADER);</di= v><div><br></div><div>like it does for Java. It partially does the ignore a= s the C++ layer only generates code for taking two arguments, but the pytho= n layer defines:</div><div><br></div><div>def something(*args):</div><div><= br></div><div>when it could generate</div><div><br></div><div>def something= (foo, bar):</div><div><br></div><div>You could raise this as an issue in Gi= thub, but at least you have a workaround.<br></div><div><br></div><div>Will= iam<br></div></div><br><div class=3D"gmail_quote gmail_quote_container"><di= v dir=3D"ltr" class=3D"gmail_attr">On Wed, 19 Feb 2025 at 10:11, Arnaud Die= deren via Swig-user <<a href=3D"mailto:[email protected]">= [email protected]</a>> wrote:<br></div><blockquote class= =3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rg= b(204,204,204);padding-left:1ex"><div dir=3D"ltr"><div dir=3D"ltr"><br></di= v><div>....aaaaaaand,<br></div><br><div class=3D"gmail_quote"><div dir=3D"l= tr" class=3D"gmail_attr">On Wed, Feb 19, 2025 at 10:42=E2=80=AFAM Arnaud Di= ederen <<a href=3D"mailto:[email protected]" target=3D"_blank">arnaud@= hex-rays.com</a>> wrote:<br></div><blockquote class=3D"gmail_quote" styl= e=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);paddin= g-left:1ex"><div dir=3D"ltr"><div><div>[...]the problem arises because of `= SOME_DEFINE_COMING_FROM_ANOTHER_HEADER`.</div></div></div></blockquote><div= ><br></div><div>Wrong again.<br><br></div><div>The problem is that the defi= ne has syntax that SWiG dislikes:</div><div><br></div><div>`#define SOME_DE= FINE_COMING_FROM_ANOTHER_HEADER thread_id_t(-1)`</div><div><br></div><div>H= owever, that is pretty good news, as I have full control over this.</div><d= iv>Looks like I can proceed with my rube-goldbergery!<br></div><div></div><= div>Best regards,</div><div>=C2=A0=C2=A0 A.<br></div><div>=C2=A0</div><bloc= kquote 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><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 t= he current, and that other module.)</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 cl= ass=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Wed, Feb 19, 2= 025 at 9:49=E2=80=AFAM Arnaud Diederen <<a href=3D"mailto:arnaud@hex-ray= s.com" target=3D"_blank">[email protected]</a>> wrote:<br></div><block= quote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1= px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr"><div><div>Hi,<= /div><div><br></div><div>we 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 n= one;display:inline-block;overflow:hidden;width:184px;height:49px"><img styl= e=3D"margin-left: 0px; margin-top: 0px;" width=3D"184" height=3D"49"></span= ></span></p><div><span style=3D"font-size:11pt;font-family:Arial,sans-serif= ;color:rgb(0,0,0);background-color:transparent;vertical-align:baseline"><sp= an style=3D"border:medium none;display:inline-block;overflow:hidden;width:1= 84px;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 none;display:inline-block;overflow:hidden;width:184px;height:49px"><img = style=3D"margin-left: 0px; margin-top: 0px;" width=3D"184" height=3D"49"></= span></span></p><div><span style=3D"font-size:11pt;font-family:Arial,sans-s= erif;color:rgb(0,0,0);background-color:transparent;vertical-align:baseline"= ><span style=3D"border:medium none;display:inline-block;overflow:hidden;wid= th:184px;height:49px"><br></span></span></div></span></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 none;display:inline-block;overflow:hidden;width:184px;height:49px"><img = style=3D"margin-left: 0px; margin-top: 0px;" width=3D"184" height=3D"49"></= span></span></p><div><span style=3D"font-size:11pt;font-family:Arial,sans-s= erif;color:rgb(0,0,0);background-color:transparent;vertical-align:baseline"= ><span style=3D"border:medium none;display:inline-block;overflow:hidden;wid= th:184px;height:49px"><br></span></span></div></span></div></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> </blockquote></div> --0000000000004a7b0b062ed2724e-- --===============7170769211647679092== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline --===============7170769211647679092== 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 --===============7170769211647679092==--