From 3b732505fa62f45cc7e46eebe08eb5bd16d4aec4 2017-12-31 14:03:05 From: Brett Smith Date: 2017-12-31 14:03:05 Subject: [PATCH] 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. --- diff --git a/oxrlib/__main__.py b/oxrlib/__main__.py index b3ef26c72ad7eed52d3d521f6f2f65a9fc7bc4c9..3ddac6fd491b749f34eb03b5b4399467b76a19d3 100644 --- a/oxrlib/__main__.py +++ b/oxrlib/__main__.py @@ -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):