Work around weird Python 3.5 generator/iterator interaction
* wrap/python/spot.py: Python 3.5 reports some unexpected SystemError messages when the stack of iterator(...(iterator(generator))) we build for random LTL generation raises a StopIteration. The messages are attached either to delete_formula or delete_randltlgenerator, claiming that these functions exit with an error; but I have checked that they do not. I've been unable to understand the cause of the issue. Replacing the generator by an iterator at least fixes the problem in a way that is transparent for our API. * wrap/python/tests/randltl.ipynb: Adjust expected formulas.
This commit is contained in:
parent
5f2d55ab2e
commit
6ff4fa9722
2 changed files with 49 additions and 26 deletions
|
|
@ -417,11 +417,10 @@ def _addfilter(fun):
|
||||||
def nfiltf(self, *args, **kwargs):
|
def nfiltf(self, *args, **kwargs):
|
||||||
it = filter(lambda f: not getattr(f, fun)(*args, **kwargs), self)
|
it = filter(lambda f: not getattr(f, fun)(*args, **kwargs), self)
|
||||||
return formulaiterator(it)
|
return formulaiterator(it)
|
||||||
setattr(formulaiterator, fun, filtf)
|
|
||||||
if fun[:3] == 'is_':
|
if fun[:3] == 'is_':
|
||||||
notfun = fun[:3] + 'not_' + fun[3:]
|
notfun = 'is_not_' + fun[3:]
|
||||||
elif fun[:4] == 'has_':
|
elif fun[:4] == 'has_':
|
||||||
notfun = fun[:4] + 'no_' + fun[4:]
|
notfun = 'has_no_' + fun[4:]
|
||||||
else:
|
else:
|
||||||
notfun = 'not_' + fun
|
notfun = 'not_' + fun
|
||||||
setattr(formulaiterator, fun, filtf)
|
setattr(formulaiterator, fun, filtf)
|
||||||
|
|
@ -433,9 +432,9 @@ def _addfilter(fun):
|
||||||
def _addmap(fun):
|
def _addmap(fun):
|
||||||
def mapf(self, *args, **kwargs):
|
def mapf(self, *args, **kwargs):
|
||||||
return formulaiterator(map(lambda f: getattr(f, fun)(*args, **kwargs),
|
return formulaiterator(map(lambda f: getattr(f, fun)(*args, **kwargs),
|
||||||
self))
|
self))
|
||||||
setattr(formula, fun, lambda self, *args, **kwargs: globals()[fun](self,
|
setattr(formula, fun,
|
||||||
*args, **kwargs))
|
lambda self, *args, **kwargs: globals()[fun](self, *args, **kwargs))
|
||||||
setattr(formulaiterator, fun, mapf)
|
setattr(formulaiterator, fun, mapf)
|
||||||
|
|
||||||
def randltl(ap, n = -1, **kwargs):
|
def randltl(ap, n = -1, **kwargs):
|
||||||
|
|
@ -531,18 +530,26 @@ def randltl(ap, n = -1, **kwargs):
|
||||||
sys.stderr.write("internal error: unknown type of output")
|
sys.stderr.write("internal error: unknown type of output")
|
||||||
return
|
return
|
||||||
|
|
||||||
def _randltlgenerator(rg):
|
class _randltliterator:
|
||||||
i = 0
|
def __init__(self, rg, n):
|
||||||
while i != n:
|
self.rg = rg
|
||||||
f = rg.next()
|
self.i = 0
|
||||||
|
self.n = n
|
||||||
|
def __iter__(self):
|
||||||
|
return self
|
||||||
|
def __next__(self):
|
||||||
|
if self.i == self.n:
|
||||||
|
raise StopIteration
|
||||||
|
f = self.rg.next()
|
||||||
if f is None:
|
if f is None:
|
||||||
sys.stderr.write("Warning: could not generate a new unique formula " \
|
sys.stderr.write("Warning: could not generate a new "
|
||||||
"after " + str(MAX_TRIALS) + " trials.\n")
|
"unique formula after {} trials.\n"
|
||||||
yield None
|
.format(MAX_TRIALS))
|
||||||
else:
|
raise StopIteration
|
||||||
yield f
|
self.i += 1
|
||||||
i += 1
|
return f
|
||||||
return formulaiterator(_randltlgenerator(rg))
|
|
||||||
|
return formulaiterator(_randltliterator(rg, n))
|
||||||
|
|
||||||
def simplify(f, **kwargs):
|
def simplify(f, **kwargs):
|
||||||
level = kwargs.get('level', None)
|
level = kwargs.get('level', None)
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,23 @@
|
||||||
{
|
{
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"name": "",
|
"kernelspec": {
|
||||||
"signature": "sha256:54b0c9570ca91322c466f914d622a3723cd3e450612a055a188927bf50dfdaf8"
|
"display_name": "Python 3",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.4.3+"
|
||||||
|
},
|
||||||
|
"name": ""
|
||||||
},
|
},
|
||||||
"nbformat": 3,
|
"nbformat": 3,
|
||||||
"nbformat_minor": 0,
|
"nbformat_minor": 0,
|
||||||
|
|
@ -164,7 +180,7 @@
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"stream": "stdout",
|
"stream": "stdout",
|
||||||
"text": [
|
"text": [
|
||||||
"G(p2 U Gp0)\n"
|
"G(p1 U Gp0)\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
@ -199,7 +215,7 @@
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"stream": "stdout",
|
"stream": "stdout",
|
||||||
"text": [
|
"text": [
|
||||||
"{{p0 && p1}[*]}<>-> (Fp1 & Fp0)\n"
|
"{{p0 && p2}[*]}<>-> (Fp2 & Fp0)\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
@ -571,11 +587,11 @@
|
||||||
"stream": "stdout",
|
"stream": "stdout",
|
||||||
"text": [
|
"text": [
|
||||||
"0\n",
|
"0\n",
|
||||||
"!(F!p0 M 1)\n",
|
"!(F!p1 M 1)\n",
|
||||||
"(Gp1 | Fp0) M 1\n",
|
"(Gp0 | Fp1) M 1\n",
|
||||||
"F!(!p0 <-> FGp0)\n",
|
"F!(!p1 <-> FGp1)\n",
|
||||||
"Gp0 U (p0 U GFp0)\n",
|
"Gp1 U (p1 U GFp1)\n",
|
||||||
"(!p0 U p0) U ((p1 & (p1 U (!p1 & (!p1 -> Fp0))) & ((!p0 U !p1) | (p0 U !p1))) | (!p1 & (!p1 U (p1 & (!p1 -> Fp0))) & ((!p0 U p1) | (p0 U p1))) | (p0 & (p0 U (!p0 & (!p1 -> Fp0))) & ((!p1 U !p0) | (p1 U !p0))) | (!p0 & (!p0 U (p0 & (!p1 -> Fp0))) & ((!p1 U p0) | (p1 U p0))) | ((!p1 -> Fp0) & (Gp1 | G!p1) & (Gp0 | G!p0)))\n"
|
"(!p1 U p1) U ((p0 & (p0 U (!p0 & (!p0 -> Fp1))) & ((!p1 U !p0) | (p1 U !p0))) | (!p0 & (!p0 U (p0 & (!p0 -> Fp1))) & ((!p1 U p0) | (p1 U p0))) | (p1 & (p1 U (!p1 & (!p0 -> Fp1))) & ((!p0 U !p1) | (p0 U !p1))) | (!p1 & (!p1 U (p1 & (!p0 -> Fp1))) & ((!p0 U p1) | (p0 U p1))) | ((!p0 -> Fp1) & (Gp0 | G!p0) & (Gp1 | G!p1)))\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue