|
31 | 31 | Here are some examples of using the decimal module: |
32 | 32 |
|
33 | 33 | >>> from decimal import * |
34 | | ->>> getcontext().prec=9 |
| 34 | +>>> setcontext(ExtendedContext) |
35 | 35 | >>> Decimal(0) |
36 | 36 | Decimal("0") |
37 | 37 | >>> Decimal("1") |
@@ -405,8 +405,8 @@ def _filterfunc(obj): |
405 | 405 |
|
406 | 406 | def setcontext(context): |
407 | 407 | """Set this thread's context to context.""" |
408 | | - if context == DefaultContext: |
409 | | - context = Context() |
| 408 | + if context in (DefaultContext, BasicContext, ExtendedContext): |
| 409 | + context = context.copy() |
410 | 410 | threading.currentThread().__decimal_context__ = context |
411 | 411 |
|
412 | 412 | def getcontext(): |
@@ -2960,28 +2960,30 @@ def _isnan(num): |
2960 | 2960 |
|
2961 | 2961 | ##### Setup Specific Contexts ################################ |
2962 | 2962 |
|
2963 | | -_basic_traps = dict.fromkeys(Signals, 1) |
2964 | | -_basic_traps.update({Inexact:0, Rounded:0, Subnormal:0}) |
2965 | | - |
2966 | 2963 | # The default context prototype used by Context() |
2967 | 2964 | # Is mutable, so than new contexts can have different default values |
2968 | 2965 |
|
| 2966 | +_default_traps = dict.fromkeys(Signals, 0) |
| 2967 | +_default_traps.update({DivisionByZero:1, Overflow:1, InvalidOperation:1}) |
| 2968 | + |
2969 | 2969 | DefaultContext = Context( |
2970 | 2970 | prec=28, rounding=ROUND_HALF_EVEN, |
2971 | | - trap_enablers=dict.fromkeys(Signals, 0), |
| 2971 | + trap_enablers=_default_traps, |
2972 | 2972 | flags=None, |
2973 | 2973 | _rounding_decision=ALWAYS_ROUND, |
2974 | 2974 | Emax=DEFAULT_MAX_EXPONENT, |
2975 | 2975 | Emin=DEFAULT_MIN_EXPONENT, |
2976 | 2976 | capitals=1 |
2977 | 2977 | ) |
2978 | | -DefaultContext.trap_enablers.update({ConversionSyntax : 1}) |
2979 | 2978 |
|
2980 | 2979 | # Pre-made alternate contexts offered by the specification |
2981 | 2980 | # Don't change these; the user should be able to select these |
2982 | 2981 | # contexts and be able to reproduce results from other implementations |
2983 | 2982 | # of the spec. |
2984 | 2983 |
|
| 2984 | +_basic_traps = dict.fromkeys(Signals, 1) |
| 2985 | +_basic_traps.update({Inexact:0, Rounded:0, Subnormal:0}) |
| 2986 | + |
2985 | 2987 | BasicContext = Context( |
2986 | 2988 | prec=9, rounding=ROUND_HALF_UP, |
2987 | 2989 | trap_enablers=_basic_traps, |
|
0 commit comments