Boost C++ Libraries: Ticket #3189: Python integer/long conversion to C++ unsigned int/long is broken https://svn.boost.org/trac10/ticket/3189 <p> The following snippet in libs/python/src/converter/builtin_converters.cpp: </p> <pre class="wiki"> return numeric_cast&lt;T&gt;( PyLong_Check(intermediate) ? PyLong_AsUnsignedLong(intermediate) : PyInt_AS_LONG(intermediate)); </pre><p> does not check if an exception was thrown by the Python/C conversion functions. Therefore, in case of error, these functions are returning -1, which is being cast to unsigned int. </p> <p> Another missing check is when the Python integer value to be converted is negative. In this case, a proper exception (such as <a class="missing wiki">ValueError</a>) must be thrown. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3189 Trac 1.4.3 anderson.lizardo@… Thu, 18 Jun 2009 13:04:11 GMT attachment set https://svn.boost.org/trac10/ticket/3189 https://svn.boost.org/trac10/ticket/3189 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost_python_unsigned_converter_fix.patch</span> </li> </ul> <p> Patch for bug <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3189" title="#3189: Patches: Python integer/long conversion to C++ unsigned int/long is broken (closed: fixed)">#3189</a> (against boost trunk) </p> Ticket anderson.lizardo@… Fri, 10 Jul 2009 20:14:01 GMT attachment set https://svn.boost.org/trac10/ticket/3189 https://svn.boost.org/trac10/ticket/3189 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost_python_unsigned_converter_fix_v2.patch</span> </li> </ul> <p> Fix builtin converters for unsigned integers (v2) </p> Ticket anderson.lizardo@… Fri, 10 Jul 2009 20:16:53 GMT <link>https://svn.boost.org/trac10/ticket/3189#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3189#comment:1</guid> <description> <p> Attached a new version (v2) of the patch. Changes: </p> <ul><li>Added more comments </li><li>Changed exception from <a class="missing wiki">ValueError</a> to <a class="missing wiki">OverflowError</a>, to match Python policy on similar conversions. </li><li>Improved test cases in libs/python/test/test_builtin_converters.py. </li></ul> </description> <category>Ticket</category> </item> <item> <author>anderson.lizardo@…</author> <pubDate>Mon, 13 Jul 2009 13:22:44 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/3189 https://svn.boost.org/trac10/ticket/3189 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost_python_unsigned_converter_fix_no_ctypes.patch</span> </li> </ul> <p> Remove dependency on ctypes for tests </p> Ticket anderson.lizardo@… Mon, 13 Jul 2009 13:27:30 GMT <link>https://svn.boost.org/trac10/ticket/3189#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3189#comment:2</guid> <description> <p> Attached a new patch (boost_python_unsigned_converter_fix_no_ctypes.patch) to be applied on top of the already applied one. </p> <p> This removes the dependency on ctypes added by previous patch. It seems ctypes is not available on all architectures/OSes, so instead add static methods to test_builtin_converters.cpp that return the sizes of the C++ types we need. </p> <p> Also do a few renames to functions added to test_builtin_converters.py so that the line length remains sane. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ralf W. Grosse-Kunstleve</dc:creator> <pubDate>Wed, 15 Jul 2009 05:13:41 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/3189#comment:3 https://svn.boost.org/trac10/ticket/3189#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Patches applied as svn revisions 54919 and 54923. Anderson, thank you very much, especially for the comprehensive tests! </p> Ticket Dave Abrahams Fri, 17 Jul 2009 19:23:12 GMT status changed; resolution deleted https://svn.boost.org/trac10/ticket/3189#comment:4 https://svn.boost.org/trac10/ticket/3189#comment:4 <ul> <li><strong>status</strong> <span class="trac-field-old">closed</span> → <span class="trac-field-new">reopened</span> </li> <li><strong>resolution</strong> <span class="trac-field-deleted">fixed</span> </li> </ul> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/3189#comment:3" title="Comment 3">rwgk</a>: </p> <blockquote class="citation"> <p> Patches applied as svn revisions 54919 and 54923. Anderson, thank you very much, especially for the comprehensive tests! </p> </blockquote> <p> Ralf, to me this looks like an incorrect bug report. numeric_cast&lt;T&gt; where T is an unsigned type is supposed to throw a bad_numeric_cast if the argument is negative. Did you verify that the supplied tests fail before applying the patch? </p> Ticket Dave Abrahams Fri, 17 Jul 2009 19:25:21 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/3189#comment:5 https://svn.boost.org/trac10/ticket/3189#comment:5 <ul> <li><strong>status</strong> <span class="trac-field-old">reopened</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Oh, I guessthe problem is that PyLong_AsUnsignedLong returns an unsigned long, thus making the result of the ?: expression unsigned. Oops. </p> Ticket