[C++-sig] argument conversion of numpy.int64 to double

Pavol Juhas pavol.juhas at gmail.com
Tue Mar 26 14:34:57 EDT 2019


Hello,

After wrapping a simple C++ function of a double argument with
boost python, I have noticed it crashes when called from Python
with a numpy.int64 value.  This is a likely situation, for example when
evaluated for values from numpy.arange.
Please see below for a minimum example.

Is there some simple way of telling the wrapper how to handle
numpy.int64 arguments?

BTW, I have noticed that the boost/numpy library sets up several scalar
converters on its initialization - this happens in boost/python/numpy.hpp
<https://github.com/boostorg/python/blob/6f72675bff276599ffdccb9681018d8b2e367bc2/include/boost/python/numpy.hpp>
 and
then in the register_scalar_converters
<https://github.com/boostorg/python/blob/6f72675bff276599ffdccb9681018d8b2e367bc2/src/numpy/dtype.cpp>
function.
However, adding a
call to boost::python::numpy::initialize did not make a difference
in my example.

Thank you for any advice.  Best,

Pavol

// dbfunction.cpp
------------------------------------------------------------

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/numpy.hpp>

double duplicate(double x)
{
    return 2 * x;
}

BOOST_PYTHON_MODULE(dbfunction)
{
    using boost::python::def;
    // no difference with line below
    // boost::python::numpy::initialize();
    def("duplicate", duplicate);
}

//
---------------------------------------------------------------------------

# TEST

$ python -c 'import dbfunction, numpy;
dbfunction.duplicate(numpy.int64(0))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
    dbfunction.duplicate(numpy.int64)
did not match C++ signature:
    duplicate(double)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20190326/1805a636/attachment.html>


More information about the Cplusplus-sig mailing list