Changeset - 3b732505fa62
[Not reviewed]
0 1 0
Brett Smith - 6 years ago 2017-12-31 14:03:05
brettcsmith@brettcsmith.org
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.
1 file changed with 11 insertions and 2 deletions:
0 comments (0 inline, 0 general)
oxrlib/__main__.py
Show inline comments
...
 
@@ -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):
0 comments (0 inline, 0 general)