diff --git a/dice/exceptions.py b/dice/exceptions.py index d3ea919..736f808 100644 --- a/dice/exceptions.py +++ b/dice/exceptions.py @@ -8,7 +8,9 @@ def from_other(cls, other): return DiceException(*other.args) elif isinstance(other, ParseFatalException): return DiceFatalException(*other.args) - return cls(*other.args) + raise NotImplementedError( + 'DiceBaseException can only wrap ParseException or ParseFatalException' + ) def pretty_print(self): string, location, description = self.args diff --git a/dice/tests/test_elements.py b/dice/tests/test_elements.py index a847b8d..78230db 100644 --- a/dice/tests/test_elements.py +++ b/dice/tests/test_elements.py @@ -1,7 +1,8 @@ from __future__ import absolute_import from dice.constants import DiceExtreme -from dice.exceptions import DiceFatalException +from dice.exceptions import DiceException, DiceFatalException +import pickle from pytest import raises import random @@ -135,6 +136,13 @@ def test_roll_error(self): rolled = roll("6d6") rolled.do_roll_single(4, 3) + def test_invalid_wrap(self): + with raises(NotImplementedError): + try: + raise RuntimeError('blah') + except Exception as e: + raise DiceException.from_other(e) + class TestEvaluate(object): def test_cache(self): @@ -191,3 +199,18 @@ def test_sysrandom_op(self): def test_sysrandom_roll(self): roll("6d6", random=self.sysrandom) + + +class TestPickle(object): + for expr in [ + '-d20', + '4d6t', + '+-(1,2,3)', + '2d20h', + '4d6h3s', + '4dF - 2', + '4*d%' + ]: + value = roll(expr, raw=True, single=False) + pickled = pickle.dumps(value) + clone = pickle.loads(pickled)