python: make sere twa::acc() survives its automaton

Fixes #468.

* python/spot/__init__.py: Add wrapper around twa::acc() and
twa::get_acceptance() to store the automaton into the acceptance
proxy, therefore ensuring that the automaton survives that proxy.
* tests/python/setacc.py: Test it.
This commit is contained in:
Alexandre Duret-Lutz 2021-07-12 15:27:07 +02:00
parent b767411a82
commit d019ea61fe
2 changed files with 35 additions and 1 deletions

View file

@ -137,6 +137,31 @@ def __om_init_new(self, str=None):
option_map.__init__ = __om_init_new option_map.__init__ = __om_init_new
__twa__acc1 = twa.acc
__twa__acc2 = twa.get_acceptance
# We store the automaton into the acceptance condition
# returned by acc so that it does not crash when
# taking the reference to a temporary, as in
# spot.translate('Foo').acc()
# Issue #468.
def __twa_acc1_tmp(self):
a = __twa__acc1(self)
a._aut = self
return a
def __twa_acc2_tmp(self):
a = __twa__acc2(self)
a._aut = self
return a
twa.acc = __twa_acc1_tmp
twa.get_acceptance = __twa_acc2_tmp
@_extend(twa, ta) @_extend(twa, ta)
class twa: class twa:
def _repr_svg_(self, opt=None): def _repr_svg_(self, opt=None):
@ -181,7 +206,6 @@ class twa:
self.highlight_edge(val, color) self.highlight_edge(val, color)
return self return self
@_extend(twa) @_extend(twa)
class twa: class twa:
def to_str(a, format='hoa', opt=None): def to_str(a, format='hoa', opt=None):

View file

@ -96,3 +96,13 @@ except RuntimeError as e:
pass pass
else: else:
raise e raise e
# issue #468
from gc import collect
acc = spot.translate('a').acc()
collect()
assert acc == spot.acc_cond('Inf(0)')
acc = spot.translate('b').get_acceptance()
collect()
assert acc == spot.acc_code('Inf(0)')