main: Decimal context sets the state of all traps.

This commit started because I noticed in the decimal documentation that
BasicContext doesn't trap decimal.Subnormal, while oxrlib probably should.
The point of this function is to set all the parts of a context that oxrlib
should have to handle currency correctly and safely.  With that motivation,
it makes more sense to set all the traps exactly as we want them, rather
than selectively setting "important" ones: they're all important.
This commit is contained in:
Brett Smith 2017-12-31 09:03:05 -05:00
parent cb17033279
commit 3b732505fa

View file

@ -7,8 +7,17 @@ import oxrlib.config
def decimal_context(base=decimal.BasicContext):
context = base.copy()
context.rounding = decimal.ROUND_HALF_EVEN
context.traps[decimal.Inexact] = False
context.traps[decimal.Rounded] = False
context.traps = {
decimal.Clamped: True,
decimal.DivisionByZero: True,
decimal.FloatOperation: True,
decimal.Inexact: False,
decimal.InvalidOperation: True,
decimal.Overflow: True,
decimal.Rounded: False,
decimal.Subnormal: True,
decimal.Underflow: True,
}
return context
def main(arglist=None, stdout=sys.stdout, stderr=sys.stderr):