[C++-sig] Expose a C++ function with an argument of template vector to Python: Boost.Python.ArgumentError

Liam Herron herron at ELLINGTON.com
Wed Nov 4 10:40:25 EST 2015


What happens if you do:

print canupo.read_msc_header(mscfile, scales, float(ptnparams))

?

From: Cplusplus-sig [mailto:cplusplus-sig-bounces+herron=ellington.com at python.org] On Behalf Of Zhan Li
Sent: Tuesday, November 03, 2015 5:13 PM
To: cplusplus-sig at python.org
Subject: [C++-sig] Expose a C++ function with an argument of template vector to Python: Boost.Python.ArgumentError

Greetings,

I'm new to Boost.Python. I have a C++ library (written and shipped by another party) that I want to wrap into Python modules so that I can use several functions in Python. One function takes an argument that is a C++ template vector. How can I export this function to a python function?

In a nutshell, here is what I'm doing now but without luck. I wrap the template vector into a Python class using Boost.Python's vector_indexing_suite. Then in the Python script, I first declare this Python class and then pass it to the function. But I got no luck and the following error message,

"
Boost.Python.ArgumentError: Python argument types in
    canupo.read_msc_header(MSCFile, FloatVec, int)
did not match C++ signature:
    read_msc_header(MSCFile {lvalue}, std::vector<float, std::allocator<float> > {lvalue}, int {lvalue})
"

More specifically, here are the C++ code and Python script I have.

In the C++ library code file "helper.hpp", it has a struct MSCFile and a function read_msc_header.

struct MSCFile
{
  // definition of MSCFile
};

int read_msc_header(MSCFile& mscfile, std::vector<FloatType>& scales, int& ptn)
{
  // implementation of read_msc_header
}

The "FloatType" is just a alias for float.

In the C++ wrapper code file "wrapper.cpp"
#include "helpers.hpp"
#include <boost/python.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/implicit.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>

BOOST_PYTHON_MODULE(canupo)
{
  using namespace boost::python;

  class_<std::vector<FloatType> >("FloatVec")
    .def(vector_indexing_suite<std::vector<FloatType> >())
    ;

  class_<MSCFile>("MSCFile", init<const char*>())
    ;

  def("read_msc_header", read_msc_header);
}

In the python script, I tried to use the function as following but failed.

import canupo
mscfile = canupo.MSCFile("msc_file_name")
scales = canupo.FloatVec()
ptnparams = 0
print canupo.read_msc_header(mscfile, scales, ptnparams)

And the error goes like this:

Boost.Python.ArgumentError: Python argument types in
    canupo.read_msc_header(MSCFile, FloatVec, int)
did not match C++ signature:
    read_msc_header(MSCFile {lvalue}, std::vector<float, std::allocator<float> > {lvalue}, int {lvalue})

I googled around but didn't find a good solution or a solution that I can understand well. Most requires rewrite the function with Boost.Python. But I don't want to change the C++ library code itself. I think there may be a good way to expose such a function to Python. Thanks a lot!

Zhan.

--
Zhan Li, 李展;
Earth & Environment, Boston University, USA;

2008 - 2011, in Chinese Academy of Sciences, China;
2004 - 2008, in Nanjing University, China;

=============================================================================================
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. 
==============================================================================================
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20151104/75113c55/attachment-0001.html>


More information about the Cplusplus-sig mailing list