[C++-sig] Re: Parameter translation python, C++

David Abrahams dave at boost-consulting.com
Wed Aug 13 17:57:20 CEST 2003


Stefan Fleiter <sf.lists at web.de> writes:

> Hi,
>
>>>when I call a boost::python method from python
>>>with a parameter of type float and there is only
>>>a C++ Method with a parameter of type double
>>>I get an "unidentifiable C++ exception".
>
>>>C++-Code:
>>>--------------------------------------------------------
>>>void pybind(db::ExtCursor& cur, int column, float value)
>
> Sorry, this is a double, I had two shells open and copied this from
> the working version.
>
>> I can't believe your compiler lets you get away with this.
> No, g++ 3.2.3 would not do that. :-)
>
>>>     py::class_< db::ExtCursor, boost::noncopyable >("ExtCursor",
>>>     py::init< db::Connection& >())
>>>         .def("bind", bind4, "bind value to query")
>>>}
>>>
>>>Python-Code:
>>>--------------------------------------------------------
>>>self._cCursor.bind(0, 2.3) // _cCursor is db::ExtCursor
>
>> Anyway, the exception is either:
>> 1. Actually a crash (most likely in your code), if you're running on
>>    windows.  Try #including libs/python/test/module_tail.cpp in your
>>    module
> I do run Linux.
> I get the exception even if pybind is defined empty ( {} ).
>
>
>> 2. A regular C++ exception thrown by your code for which you've not
>>    installed a translator.
> See above, an empty method can't throw an exception.
>
> And last but not least:
> If I change "double" to "float" everything is ok.
>

Honestly, I don't see how this could have anything to do with
Boost.Python.  If it doesn't fail on the following code, I think it
has to be in your code.  If it does, I think it has to be a compiler
bug.

#include <boost/python.hpp>

namespace db
{
  struct ExtCursor
  {
      ExtCursor(int) {}
  };
}

void pybind(db::ExtCursor& cur, int column, double value)
{
}

namespace py = boost::python;

BOOST_PYTHON_MODULE(foo_ext)
{

     void (*bind4)(db::ExtCursor&, int, double) = &pybind;

     py::class_< db::ExtCursor, boost::noncopyable >(
         "ExtCursor", py::init< int >())
         .def("bind", bind4, "bind value to query")
         ;
}
----------
from foo_ext import *
print ExtCursor(3).bind(0, 2.3)


-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list