From 3b732505fa62f45cc7e46eebe08eb5bd16d4aec4 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Sun, 31 Dec 2017 09:03:05 -0500 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. --- oxrlib/__main__.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/oxrlib/__main__.py b/oxrlib/__main__.py index b3ef26c..3ddac6f 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):