Boost C++ Libraries: Ticket #4726: Bugs when using named args. https://svn.boost.org/trac10/ticket/4726 <p> There seem to be issues when naming function arguments. Same thing happens for class_ defs and init&lt;&gt; functions. </p> <p> Using non-named args <code>f(1,2,3)</code> works fine in all cases below but using named args <code>f(1,2,z=3)</code> works or fails depending on boost python definition. </p> <p> The first two of the following defs fail when using named args. The first one crashes python and the second raises "did not match C++ signature" exception. The third version works as expected. </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;boost/python/module.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/python/def.hpp&gt;</span><span class="cp"></span> <span class="kt">int</span> <span class="nf">f</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">,</span> <span class="kt">int</span> <span class="n">z</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="o">*</span><span class="n">z</span><span class="p">;}</span> <span class="n">BOOST_PYTHON_MODULE</span><span class="p">(</span><span class="n">_pylib</span><span class="p">)</span> <span class="p">{</span> <span class="k">namespace</span> <span class="n">bp</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">python</span><span class="p">;</span> <span class="c1">// Bug</span> <span class="n">bp</span><span class="o">::</span><span class="n">def</span><span class="p">(</span><span class="s">&quot;bug1&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">f</span><span class="p">,</span> <span class="p">(</span><span class="s">&quot;x&quot;</span><span class="p">,</span> <span class="s">&quot;y&quot;</span><span class="p">,</span> <span class="n">bp</span><span class="o">::</span><span class="n">arg</span><span class="p">(</span><span class="s">&quot;z&quot;</span><span class="p">)));</span> <span class="n">bp</span><span class="o">::</span><span class="n">def</span><span class="p">(</span><span class="s">&quot;bug2&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">f</span><span class="p">,</span> <span class="p">(</span><span class="s">&quot;x&quot;</span><span class="p">,</span> <span class="s">&quot;y&quot;</span><span class="p">,</span> <span class="s">&quot;z&quot;</span><span class="p">));</span> <span class="c1">// Works </span> <span class="n">bp</span><span class="o">::</span><span class="n">def</span><span class="p">(</span><span class="s">&quot;works_fine&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">f</span><span class="p">,</span> <span class="p">(</span><span class="n">bp</span><span class="o">::</span><span class="n">arg</span><span class="p">(</span><span class="s">&quot;x&quot;</span><span class="p">),</span> <span class="n">bp</span><span class="o">::</span><span class="n">arg</span><span class="p">(</span><span class="s">&quot;y&quot;</span><span class="p">),</span> <span class="n">bp</span><span class="o">::</span><span class="n">arg</span><span class="p">(</span><span class="s">&quot;z&quot;</span><span class="p">)));</span> <span class="p">}</span> </pre></div></div><p> Running <code> bug1(x=1, y=2, z=3) </code> crashes python and <code>bug2(1, 2, z=3)</code> gives an <code>ArgumentError</code> </p> <pre class="wiki">Boost.Python.ArgumentError: Python argument types in _pylib.bug2(int, int) did not match C++ signature: bug2(int, int, int) </pre><p> Summary of errors: </p> <div class="wiki-code"><div class="code"><pre><span class="kn">from</span> <span class="nn">_pylib</span> <span class="kn">import</span> <span class="o">*</span> <span class="n">works_fine</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># OK</span> <span class="n">bug1</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># OK</span> <span class="n">bug2</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># OK</span> <span class="n">works_fine</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">z</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># OK</span> <span class="n">bug1</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">z</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># OK</span> <span class="n">bug2</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">z</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># Error: did not match C++ signature</span> <span class="n">works_fine</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">z</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># OK</span> <span class="n">bug1</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">z</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># Error: Python crashes and exits abnormally</span> <span class="n">bug2</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">z</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># Error: did not match C++ signature</span> </pre></div></div><p> I'm using: </p> <ul><li>boost 1.44. <code>BoostPro</code> Windows pre-built binary for VC++ 2008. </li><li>Python 2.6 <ul><li><code>sys.version = '2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)]')</code> </li></ul></li><li>Microsoft VC++ 2008 </li></ul> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4726 Trac 1.4.3 Dave Abrahams Mon, 11 Oct 2010 15:28:37 GMT <link>https://svn.boost.org/trac10/ticket/4726#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4726#comment:1</guid> <description> <p> It's probably a bug that you're able to crash Python, but the two examples you are reporting as bugs don't mean what you think they do: </p> <div class="wiki-code"><div class="code"><pre> <span class="n">bp</span><span class="o">::</span><span class="n">def</span><span class="p">(</span><span class="s">&quot;bug1&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">f</span><span class="p">,</span> <span class="p">(</span><span class="s">&quot;x&quot;</span><span class="p">,</span> <span class="s">&quot;y&quot;</span><span class="p">,</span> <span class="n">bp</span><span class="o">::</span><span class="n">arg</span><span class="p">(</span><span class="s">&quot;z&quot;</span><span class="p">)));</span> <span class="n">bp</span><span class="o">::</span><span class="n">def</span><span class="p">(</span><span class="s">&quot;bug2&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">f</span><span class="p">,</span> <span class="p">(</span><span class="s">&quot;x&quot;</span><span class="p">,</span> <span class="s">&quot;y&quot;</span><span class="p">,</span> <span class="s">&quot;z&quot;</span><span class="p">));</span> </pre></div></div><p> In particular, because the comma operator associates from left-to-right, what you've written there is equivalent to: </p> <div class="wiki-code"><div class="code"><pre> <span class="n">bp</span><span class="o">::</span><span class="n">def</span><span class="p">(</span><span class="s">&quot;bug1&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">f</span><span class="p">,</span> <span class="n">bp</span><span class="o">::</span><span class="n">arg</span><span class="p">(</span><span class="s">&quot;z&quot;</span><span class="p">));</span> <span class="n">bp</span><span class="o">::</span><span class="n">def</span><span class="p">(</span><span class="s">&quot;bug2&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">f</span><span class="p">,</span> <span class="s">&quot;z&quot;</span><span class="p">);</span> </pre></div></div> </description> <category>Ticket</category> </item> <item> <author>Albin Thoren <thorena@…></author> <pubDate>Wed, 13 Oct 2010 07:47:22 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4726#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4726#comment:2</guid> <description> <p> Yes, you're right. I should have seen that. </p> <p> The crash then only occurs with unintended use of bp::arg. That is, if the number of function arguments does not match the number of bp::arg(s). That is: </p> <div class="wiki-code"><div class="code"><pre><span class="n">bp</span><span class="o">::</span><span class="n">def</span><span class="p">(</span><span class="s">&quot;bug&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">f</span><span class="p">,</span> <span class="p">(</span><span class="n">bp</span><span class="o">::</span><span class="n">arg</span><span class="p">(</span><span class="s">&quot;y&quot;</span><span class="p">),</span> <span class="n">bp</span><span class="o">::</span><span class="n">arg</span><span class="p">(</span><span class="s">&quot;z&quot;</span><span class="p">)));</span> </pre></div></div><p> and </p> <div class="wiki-code"><div class="code"><pre><span class="n">bug</span><span class="p">(</span><span class="n">anything</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">z</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span> </pre></div></div><p> also crashes python. </p> <p> I assume any declaration with mismatching num of args should be illegal/undefined behaviour. Would it be possible to add a static assert to verify that the number or function arguments == the number of bp::arg(s)? </p> </description> <category>Ticket</category> </item> </channel> </rss>