allow for defensive behaviour in konsume

This commit is contained in:
Dima Gerasimov 2020-04-13 13:12:46 +01:00 committed by karlicoss
parent f3288f7cf0
commit 697c3bb851

View file

@ -106,22 +106,28 @@ def _wrap(j, parent=None):
res = Wvalue(parent, j) res = Wvalue(parent, j)
return res, [res] return res, [res]
else: else:
raise RuntimeError(str(j)) raise RuntimeError(f'Unexpected type: {type(j)} {j}')
from contextlib import contextmanager from contextlib import contextmanager
class UnconsumedError(Exception): class UnconsumedError(Exception):
pass pass
# TODO think about error policy later...
@contextmanager @contextmanager
def wrap(j): def wrap(j, throw=True):
w, children = _wrap(j) w, children = _wrap(j)
yield w yield w
for c in children: for c in children:
if not c.this_consumed(): # TODO hmm. how does it figure out if it's consumed??? if not c.this_consumed(): # TODO hmm. how does it figure out if it's consumed???
if throw:
raise UnconsumedError(str(c)) raise UnconsumedError(str(c))
else:
# TODO log?
pass
def test_unconsumed(): def test_unconsumed():
import pytest # type: ignore import pytest # type: ignore