From 8a26744720b9909cab675ee438af1fe3617be437 Mon Sep 17 00:00:00 2001 From: Alexandre Duret-Lutz Date: Thu, 2 Aug 2018 23:05:22 +0200 Subject: [PATCH] fix python bindings for spot::parsed_formula::f getter * python/spot/impl.i: Add a typemap. * tests/python/ltlsimple.py: Add a test case for an issue. * NEWS: Mention the bug. --- NEWS | 4 ++++ python/spot/impl.i | 9 +++++++++ tests/python/ltlsimple.py | 8 ++++++++ 3 files changed, 21 insertions(+) diff --git a/NEWS b/NEWS index c7aba77a9..fcc89e53e 100644 --- a/NEWS +++ b/NEWS @@ -63,6 +63,10 @@ New in spot 2.6.0.dev (not yet released) documentation was also matching the code, so this is a backward incompatible change. + - The Python binding of the getter of spot::parsed_formula::f was + returning a reference instead of a copy, causing issues if the + reference outlasted the parsed_formula struct. + New in spot 2.6 (2018-07-04) diff --git a/python/spot/impl.i b/python/spot/impl.i index 23a1dbc27..6bc5af3c3 100644 --- a/python/spot/impl.i +++ b/python/spot/impl.i @@ -386,6 +386,15 @@ namespace swig SWIG_POINTER_IMPLICIT_CONV)); } +// Access to stucture members, such as spot::parsed_formula::f are done via +// the pointer typemap. We want to enforce a copy. +%typemap(out) spot::formula* { + if (!*$1) + $result = SWIG_Py_Void(); + else + $result = SWIG_NewPointerObj(new spot::formula(*$1), $descriptor(spot::formula*), SWIG_POINTER_OWN); +} + %typemap(out) spot::formula { if (!$1) $result = SWIG_Py_Void(); diff --git a/tests/python/ltlsimple.py b/tests/python/ltlsimple.py index 96ebcda17..a8443a357 100755 --- a/tests/python/ltlsimple.py +++ b/tests/python/ltlsimple.py @@ -134,3 +134,11 @@ for (input, output) in [('(a&b)<->b', 'b->(a&b)'), print(input, f, output) assert(f == output) assert(spot.are_equivalent(input, output)) + +def myparse(input): + env = spot.default_environment.instance() + pf = spot.parse_infix_psl(input, env) + return pf.f +# This used to fail, because myparse would return a pointer +# to pf.f inside the destroyed pf. +assert myparse('a U b') == spot.formula('a U b')