[Python-checkins] python/dist/src/Lib decimal.py,1.14,1.15
rhettinger at users.sourceforge.net
rhettinger at users.sourceforge.net
Sat Jul 10 16:14:39 CEST 2004
Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30067/Lib
Modified Files:
decimal.py
Log Message:
Improve Context construction and representation:
* Rename "trap_enablers" to just "traps".
* Simplify names of "settraps" and "setflags" to just "traps" and "flags".
* Show "capitals" in the context representation
* Simplify the Context constructor to match its repr form so that only
the set flags and traps need to be listed.
* Representation can now be run through eval().
Improve the error message when the Decimal constructor is given a float.
The test suite no longer needs a duplicate reset_flags method.
Index: decimal.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/decimal.py,v
retrieving revision 1.14
retrieving revision 1.15
diff -C2 -d -r1.14 -r1.15
*** decimal.py 9 Jul 2004 10:52:54 -0000 1.14
--- decimal.py 10 Jul 2004 14:14:37 -0000 1.15
***************
*** 71,75 ****
>>> print dig / 0
Infinity
! >>> getcontext().trap_enablers[DivisionByZero] = 1
>>> print dig / 0
Traceback (most recent call last):
--- 71,75 ----
>>> print dig / 0
Infinity
! >>> getcontext().traps[DivisionByZero] = 1
>>> print dig / 0
Traceback (most recent call last):
***************
*** 79,88 ****
DivisionByZero: x / 0
>>> c = Context()
! >>> c.trap_enablers[InvalidOperation] = 0
>>> print c.flags[InvalidOperation]
0
>>> c.divide(Decimal(0), Decimal(0))
Decimal("NaN")
! >>> c.trap_enablers[InvalidOperation] = 1
>>> print c.flags[InvalidOperation]
1
--- 79,88 ----
DivisionByZero: x / 0
>>> c = Context()
! >>> c.traps[InvalidOperation] = 0
>>> print c.flags[InvalidOperation]
0
>>> c.divide(Decimal(0), Decimal(0))
Decimal("NaN")
! >>> c.traps[InvalidOperation] = 1
>>> print c.flags[InvalidOperation]
1
***************
*** 99,103 ****
1
>>> c.flags[InvalidOperation] = 0
! >>> c.trap_enablers[InvalidOperation] = 0
>>> print c.divide(Decimal(0), Decimal(0))
NaN
--- 99,103 ----
1
>>> c.flags[InvalidOperation] = 0
! >>> c.traps[InvalidOperation] = 0
>>> print c.divide(Decimal(0), Decimal(0))
NaN
***************
*** 496,500 ****
return
! raise TypeError("Can't convert %r" % value)
def _convert_other(self, other):
--- 496,504 ----
return
! if isinstance(value, float):
! raise TypeError("Cannot convert float to Decimal. " +
! "First convert the float to a string")
!
! raise TypeError("Cannot convert %r" % value)
def _convert_other(self, other):
***************
*** 2097,2101 ****
rounding - rounding type. (how you round)
_rounding_decision - ALWAYS_ROUND, NEVER_ROUND -- do you round?
! trap_enablers - If trap_enablers[exception] = 1, then the exception is
raised when it is caused. Otherwise, a value is
substituted in.
--- 2101,2105 ----
rounding - rounding type. (how you round)
_rounding_decision - ALWAYS_ROUND, NEVER_ROUND -- do you round?
! traps - If traps[exception] = 1, then the exception is
raised when it is caused. Otherwise, a value is
substituted in.
***************
*** 2111,2121 ****
def __init__(self, prec=None, rounding=None,
! trap_enablers=None, flags=None,
_rounding_decision=None,
Emin=None, Emax=None,
capitals=None, _clamp=0,
_ignored_flags=[]):
! if flags is None:
! flags = dict.fromkeys(Signals, 0)
for name, val in locals().items():
if val is None:
--- 2115,2127 ----
def __init__(self, prec=None, rounding=None,
! traps=None, flags=[],
_rounding_decision=None,
Emin=None, Emax=None,
capitals=None, _clamp=0,
_ignored_flags=[]):
! if not isinstance(flags, dict):
! flags = dict([(s,s in flags) for s in Signals])
! if traps is not None and not isinstance(traps, dict):
! traps = dict([(s,s in traps) for s in Signals])
for name, val in locals().items():
if val is None:
***************
*** 2126,2134 ****
def __repr__(self):
! """Show the current context in readable form, not in a form for eval()."""
s = []
! s.append('Context(prec=%(prec)d, rounding=%(rounding)s, Emin=%(Emin)d, Emax=%(Emax)d' % vars(self))
! s.append('setflags=%r' % [f.__name__ for f, v in self.flags.items() if v])
! s.append('settraps=%r' % [t.__name__ for t, v in self.trap_enablers.items() if v])
return ', '.join(s) + ')'
--- 2132,2140 ----
def __repr__(self):
! """Show the current context."""
s = []
! s.append('Context(prec=%(prec)d, rounding=%(rounding)s, Emin=%(Emin)d, Emax=%(Emax)d, capitals=%(capitals)d' % vars(self))
! s.append('flags=[' + ', '.join([f.__name__ for f, v in self.flags.items() if v]) + ']')
! s.append('traps=[' + ', '.join([t.__name__ for t, v in self.traps.items() if v]) + ']')
return ', '.join(s) + ')'
***************
*** 2140,2144 ****
def copy(self):
"""Returns a copy from self."""
! nc = Context(self.prec, self.rounding, self.trap_enablers, self.flags,
self._rounding_decision, self.Emin, self.Emax,
self.capitals, self._clamp, self._ignored_flags)
--- 2146,2150 ----
def copy(self):
"""Returns a copy from self."""
! nc = Context(self.prec, self.rounding, self.traps, self.flags,
self._rounding_decision, self.Emin, self.Emax,
self.capitals, self._clamp, self._ignored_flags)
***************
*** 2160,2164 ****
self.flags[error] += 1
! if not self.trap_enablers[error]:
#The errors define how to handle themselves.
return condition().handle(self, *args)
--- 2166,2170 ----
self.flags[error] += 1
! if not self.traps[error]:
#The errors define how to handle themselves.
return condition().handle(self, *args)
***************
*** 2947,2957 ****
# Is mutable, so than new contexts can have different default values
- _default_traps = dict.fromkeys(Signals, 0)
- _default_traps.update({DivisionByZero:1, Overflow:1, InvalidOperation:1})
-
DefaultContext = Context(
prec=28, rounding=ROUND_HALF_EVEN,
! trap_enablers=_default_traps,
! flags=None,
_rounding_decision=ALWAYS_ROUND,
Emax=DEFAULT_MAX_EXPONENT,
--- 2953,2960 ----
# Is mutable, so than new contexts can have different default values
DefaultContext = Context(
prec=28, rounding=ROUND_HALF_EVEN,
! traps=[DivisionByZero, Overflow, InvalidOperation],
! flags=[],
_rounding_decision=ALWAYS_ROUND,
Emax=DEFAULT_MAX_EXPONENT,
***************
*** 2965,2975 ****
# of the spec.
- _basic_traps = dict.fromkeys(Signals, 1)
- _basic_traps.update({Inexact:0, Rounded:0, Subnormal:0})
-
BasicContext = Context(
prec=9, rounding=ROUND_HALF_UP,
! trap_enablers=_basic_traps,
! flags=None,
_rounding_decision=ALWAYS_ROUND,
)
--- 2968,2975 ----
# of the spec.
BasicContext = Context(
prec=9, rounding=ROUND_HALF_UP,
! traps=[DivisionByZero, Overflow, InvalidOperation, Clamped, Underflow],
! flags=[],
_rounding_decision=ALWAYS_ROUND,
)
***************
*** 2977,2982 ****
ExtendedContext = Context(
prec=9, rounding=ROUND_HALF_EVEN,
! trap_enablers=dict.fromkeys(Signals, 0),
! flags=None,
_rounding_decision=ALWAYS_ROUND,
)
--- 2977,2982 ----
ExtendedContext = Context(
prec=9, rounding=ROUND_HALF_EVEN,
! traps=[],
! flags=[],
_rounding_decision=ALWAYS_ROUND,
)
More information about the Python-checkins
mailing list