Exporting wrapped functions doesn't work
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 38/100
Research direction
Start by reproducing the wrapped-function examples in the issue, then inspect the boost::python::def entry point and its handling of callable objects. Done means the wrapped add2 and add examples can be exported successfully, matching the cases that already work directly.
Written by the indexing model from the issue text.
Description
I find myself in need of exporting a whole lot of functions to Python which require a wrapper (the API needs locking and whatnot).
The following code probably should work (pybind11 can handle it, for the most part), but doesn't.
#include <iostream>
#include <boost/python.hpp>
template <class R, class... Args>
struct wrap
{
using funct_type = R(*)(Args...);
funct_type func;
wrap(funct_type f): func(f) {};
R operator()(Args&&... args)
{
std::cout << "before calling\n";
R ret=func(std::forward<Args>(args)...);
std::cout << "After calling\n";
return ret;
}
};
class bar {};
class foo : bar {
public:
enum xfoo { FOO=1, BAR=2, BAZ=3 };
static int add(int a, int b, enum xfoo c) { return a+b; }
static int add2(int a, int b) { return a+b; }
};
BOOST_PYTHON_MODULE(example)
{
using namespace boost::python;
// this works
std::cout << "Result" << wrap{foo::add}(1,2, foo::xfoo::FOO) << std::endl;
// so does this
def("add", foo::add, "A function that adds two numbers and no enum");
// this works with pybind11 but not boost
def("add2", wrap{foo::add2}, "A function that adds two numbers");
// this works with neither
def("add", wrap{foo::add}, "A function that adds two numbers and no enum");
}
- Dominant language
- C++
- Stars
- 537
- Forks
- 223
- Avg merge
- 11h 22m
- Merged PRs (30d)
- 2
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from boostorg/python
-
Difficulty 1/5 Under an hour Newbie friendliness 84/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
-
BoostDetectToolset-1.90.0.cmake file not found in an include() call in boost_python-config.cmake Open
Difficulty 3/5 1-2 days Newbie friendliness 48/100
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Similar issues
-
enhancement
Difficulty 1/5 Under an hour Newbie friendliness 88/100
QuantStack/git2cpp#187 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
mlcommons/mobile_app_open#1182 ·
-
Needs-Triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
microsoft/winget-cli#6547 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
AXERA-TECH/ax-llm#77 ·