Redesigned virtual call mechanism and user-facing syntax (breaking change!)
Sergey Lyskov pointed out that the trampoline mechanism used to override
virtual methods from within Python caused unnecessary overheads when
instantiating the original (i.e. non-extended) class.
This commit removes this inefficiency, but some syntax changes were
needed to achieve this. Projects using this features will need to make a
few changes:
In particular, the example below shows the old syntax to instantiate a
class with a trampoline:
class_<TrampolineClass>("MyClass")
.alias<MyClass>()
....
This is what should be used now:
class_<MyClass, std::unique_ptr<MyClass, TrampolineClass>("MyClass")
....
Importantly, the trampoline class is now specified as the *third*
argument to the class_ template, and the alias<..>() call is gone. The
second argument with the unique pointer is simply the default holder
type used by pybind11.
diff --git a/example/example12.cpp b/example/example12.cpp
index 5cc8dc8..e5555f5 100644
--- a/example/example12.cpp
+++ b/example/example12.cpp
@@ -82,15 +82,11 @@
}
void init_ex12(py::module &m) {
- /* Important: use the wrapper type as a template
- argument to class_<>, but use the original name
- to denote the type */
- py::class_<PyExample12>(m, "Example12")
- /* Declare that 'PyExample12' is really an alias for the original type 'Example12' */
- .alias<Example12>()
+ /* Important: indicate the trampoline class PyExample12 using the third
+ argument to py::class_. The second argument with the unique pointer
+ is simply the default holder type used by pybind11. */
+ py::class_<Example12, std::unique_ptr<Example12>, PyExample12>(m, "Example12")
.def(py::init<int>())
- /* Copy constructor (not needed in this case, but should generally be declared in this way) */
- .def(py::init<const PyExample12 &>())
/* Reference original class in function definitions */
.def("run", &Example12::run)
.def("run_bool", &Example12::run_bool)
diff --git a/example/issues.cpp b/example/issues.cpp
index d1a1941..e164708 100644
--- a/example/issues.cpp
+++ b/example/issues.cpp
@@ -42,8 +42,7 @@
}
};
- py::class_<DispatchIssue> base(m2, "DispatchIssue");
- base.alias<Base>()
+ py::class_<Base, std::unique_ptr<Base>, DispatchIssue>(m2, "DispatchIssue")
.def(py::init<>())
.def("dispatch", &Base::dispatch);
@@ -108,4 +107,28 @@
// (no id): don't cast doubles to ints
m2.def("expect_float", [](float f) { return f; });
m2.def("expect_int", [](int i) { return i; });
+
+ // (no id): don't invoke Python dispatch code when instantiating C++
+ // classes that were not extended on the Python side
+ struct A {
+ virtual ~A() {}
+ virtual void f() { std::cout << "A.f()" << std::endl; }
+ };
+
+ struct PyA : A {
+ PyA() { std::cout << "PyA.PyA()" << std::endl; }
+
+ void f() override {
+ std::cout << "PyA.f()" << std::endl;
+ PYBIND11_OVERLOAD(void, A, f);
+ }
+ };
+
+ auto call_f = [](A *a) { a->f(); };
+
+ pybind11::class_<A, std::unique_ptr<A>, PyA>(m2, "A")
+ .def(py::init<>())
+ .def("f", &A::f);
+
+ m2.def("call_f", call_f);
}
diff --git a/example/issues.py b/example/issues.py
index d075e83..257f08e 100644
--- a/example/issues.py
+++ b/example/issues.py
@@ -9,6 +9,7 @@
from example.issues import iterator_passthrough
from example.issues import ElementList, ElementA, print_element
from example.issues import expect_float, expect_int
+from example.issues import A, call_f
import gc
print_cchar("const char *")
@@ -55,3 +56,19 @@
print("Failed as expected: " + str(e))
print(expect_float(12))
+
+class B(A):
+ def __init__(self):
+ super(B, self).__init__()
+
+ def f(self):
+ print("In python f()")
+
+print("C++ version")
+a = A()
+call_f(a)
+
+print("Python version")
+b = B()
+call_f(b)
+
diff --git a/example/issues.ref b/example/issues.ref
index 4888ea5..58cc798 100644
--- a/example/issues.ref
+++ b/example/issues.ref
@@ -12,3 +12,9 @@
1. (int) -> int
Invoked with: 5.2
12.0
+C++ version
+A.f()
+Python version
+PyA.PyA()
+PyA.f()
+In python f()