From 9cfa71f4118b8281b21bacbeba23d8c4d1f90076 Mon Sep 17 00:00:00 2001 From: jmabille Date: Tue, 23 Feb 2016 22:41:07 +0100 Subject: [PATCH] Example of bug in functions returning bool overriden in python --- example/example12.cpp | 15 +++++++++++++++ example/example12.py | 7 ++++++- example/example12.ref | 2 ++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/example/example12.cpp b/example/example12.cpp index 57a0765c4..7bba2c8f9 100644 --- a/example/example12.cpp +++ b/example/example12.cpp @@ -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()) /* 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); } diff --git a/example/example12.py b/example/example12.py index 4f785750b..eb175239f 100644 --- a/example/example12.py +++ b/example/example12.py @@ -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) diff --git a/example/example12.ref b/example/example12.ref index be6d09211..2274cddd2 100644 --- a/example/example12.ref +++ b/example/example12.ref @@ -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..