Example of bug in functions returning bool overriden in python

This commit is contained in:
jmabille 2016-02-23 22:41:07 +01:00 committed by Wenzel Jakob
parent 347e6eaf68
commit 9cfa71f411
3 changed files with 23 additions and 1 deletions

View File

@ -27,6 +27,7 @@ public:
return state + value;
}
virtual bool run_bool() = 0;
virtual void pure_virtual() = 0;
private:
int state;
@ -47,6 +48,14 @@ public:
);
}
virtual bool run_bool() {
PYBIND11_OVERLOAD_PURE(
bool,
Example12,
run_bool
);
}
virtual void pure_virtual() {
PYBIND11_OVERLOAD_PURE(
void, /* Return type */
@ -61,6 +70,10 @@ int runExample12(Example12 *ex, int value) {
return ex->run(value);
}
bool runExample12Bool(Example12* ex) {
return ex->run_bool();
}
void runExample12Virtual(Example12 *ex) {
ex->pure_virtual();
}
@ -75,8 +88,10 @@ void init_ex12(py::module &m) {
.def(py::init<int>())
/* Reference original class in function definitions */
.def("run", &Example12::run)
.def("run_bool", &Example12::run_bool)
.def("pure_virtual", &Example12::pure_virtual);
m.def("runExample12", &runExample12);
m.def("runExample12Bool", &runExample12Bool);
m.def("runExample12Virtual", &runExample12Virtual);
}

View File

@ -3,7 +3,7 @@ from __future__ import print_function
import sys
sys.path.append('.')
from example import Example12, runExample12, runExample12Virtual
from example import Example12, runExample12, runExample12Virtual, runExample12Bool
class ExtendedExample12(Example12):
@ -15,6 +15,10 @@ class ExtendedExample12(Example12):
print('ExtendedExample12::run(%i), calling parent..' % value)
return super(ExtendedExample12, self).run(value + 1)
def run_bool(self):
print('ExtendedExample12::run_bool()')
return False
def pure_virtual(self):
print('ExtendedExample12::pure_virtual(): %s' % self.data)
@ -28,4 +32,5 @@ except Exception as e:
ex12p = ExtendedExample12(10)
print(runExample12(ex12p, 20))
print(runExample12Bool(ex12p))
runExample12Virtual(ex12p)

View File

@ -6,6 +6,8 @@ Constructing Example12..
ExtendedExample12::run(20), calling parent..
Original implementation of Example12::run(state=11, value=21)
32
ExtendedExample12::run_bool()
False
ExtendedExample12::pure_virtual(): Hello world
Destructing Example12..
Destructing Example12..