BoostPython: C++ function with "non-const double reference" argument issue. Python looks for float causing argument mismatch.
Here is my example: ------------------------------------------------------------------------------------------- CPP Code: ------------------------------------------------------------------------------------------- #include <boost/python.hpp> using namespace boost::python; void updateDoubleReference(double& x) { x = x + 5.4321; } BOOST_PYTHON_MODULE(_testDR) { // Add regular functions to the module. def("updateDoubleReference", &updateDoubleReference ); } ------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------- PY Code: ------------------------------------------------------------------------------------------- #!/usr/bin/python # # from _testDR import * x = 3.0 updateDoubleReference(x) -------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------- ERROR OUTPUT ------------------------------------------------------------------------------------------- python2.5 testDR.py Traceback (most recent call last): File "testDR.py", line 8, in <module> updateDoubleReference(x) Boost.Python.ArgumentError: Python argument types in _testDR.updateDoubleReference(float) did not match C++ signature: updateDoubleReference(double {lvalue}) ------------------------------------------------------------------------------------------- How do people typically handle this issue? Wrap the function with 'float' versions of the arguments? I tried to create a 'NativeDouble' struct that had a double member attribute but when exposed to python, it simply interprets this as a float and it still has a mismatch. Any help would be greatly appreciated. Thanks, Liam ============================================================================================= Email transmissions can not be guaranteed to be secure or error-free, as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message which arise as a result of email transmission. In addition, the information contained in this email message is intended only for use of the individual or entity named above. If the reader of this message is not the intended recipient, or the employee or agent responsible to deliver it to the intended recipient, you are hereby notified that any dissemination, distribution,or copying of this communication, disclosure of the parties to it, or any action taken or omitted to be taken in reliance on it, is strictly prohibited, and may be unlawful. If you are not the intended recipient please delete this email message. ==============================================================================================
The problem here isn't a mismatch between double and float, it's that Python's float object is immutable, and hence there's no way to translate it into a C++ lvalue. You simply can't wrap a C++ function with a "double &" (or "int &" or "string &") argument to Python with the same signature, regardless of whether you're using Boost.Python or something else, because Python won't allow the argument you pass to be changed. You have two options: - Add an extra struct (similar to your NativeDouble idea) that is a wrapped class that only contains a double. But you'll need explicit get/set methods, not just a public data member, though you can add a Python property to make the get/set look like a data member. - Transform the function signature, by wrapping a new function that takes a non-reference double and returns the new one, delegating it's work to the original function. Jim
participants (2)
-
Jim Bosch -
Liam Herron