[Python-checkins] python/dist/src/Lib decimal.py,1.12,1.13
rhettinger at users.sourceforge.net
rhettinger at users.sourceforge.net
Fri Jul 9 12:03:29 CEST 2004
Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28156/Lib
Modified Files:
decimal.py
Log Message:
Module and tests:
* Map conditions to related signals.
* Make contexts unhashable.
* Eliminate used "default" attribute in exception definitions.
* Eliminate the _filterfunc in favor of a straight list.
Docs:
* Eliminate documented references to conditions that are not signals.
* Eliminate parenthetical notes such as "1/0 --> Inf" which are no
longer true with the new defaults.
Index: decimal.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/decimal.py,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** decimal.py 8 Jul 2004 00:49:18 -0000 1.12
--- decimal.py 9 Jul 2004 10:02:51 -0000 1.13
***************
*** 79,92 ****
DivisionByZero: x / 0
>>> c = Context()
! >>> c.trap_enablers[DivisionUndefined] = 0
! >>> print c.flags[DivisionUndefined]
0
>>> c.divide(Decimal(0), Decimal(0))
Decimal("NaN")
! >>> c.trap_enablers[DivisionUndefined] = 1
! >>> print c.flags[DivisionUndefined]
1
! >>> c.flags[DivisionUndefined] = 0
! >>> print c.flags[DivisionUndefined]
0
>>> print c.divide(Decimal(0), Decimal(0))
--- 79,92 ----
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
! >>> c.flags[InvalidOperation] = 0
! >>> print c.flags[InvalidOperation]
0
>>> print c.divide(Decimal(0), Decimal(0))
***************
*** 95,106 ****
...
...
! DivisionUndefined: 0 / 0
! >>> print c.flags[DivisionUndefined]
1
! >>> c.flags[DivisionUndefined] = 0
! >>> c.trap_enablers[DivisionUndefined] = False
>>> print c.divide(Decimal(0), Decimal(0))
NaN
! >>> print c.flags[DivisionUndefined]
1
>>>
--- 95,106 ----
...
...
! InvalidOperation: 0 / 0
! >>> print c.flags[InvalidOperation]
1
! >>> c.flags[InvalidOperation] = 0
! >>> c.trap_enablers[InvalidOperation] = 0
>>> print c.divide(Decimal(0), Decimal(0))
NaN
! >>> print c.flags[InvalidOperation]
1
>>>
***************
*** 153,157 ****
class DecimalException(ArithmeticError):
! """Base exception class, defines default things.
Used exceptions derive from this.
--- 153,157 ----
class DecimalException(ArithmeticError):
! """Base exception class.
Used exceptions derive from this.
***************
*** 161,170 ****
anything, though.
- Attributes:
-
- default -- If the context is basic, the trap_enablers are set to
- this by default. Extended contexts start out with them set
- to 0, regardless.
-
handle -- Called when context._raise_error is called and the
trap_enabler is set. First argument is self, second is the
--- 161,164 ----
***************
*** 177,181 ****
from DecimalException.
"""
- default = 1
def handle(self, context, *args):
pass
--- 171,174 ----
***************
*** 289,293 ****
operation (or sequence of operations) was inexact.
"""
! default = 0
class InvalidContext(InvalidOperation):
--- 282,286 ----
operation (or sequence of operations) was inexact.
"""
! pass
class InvalidContext(InvalidOperation):
***************
*** 316,320 ****
operation (or sequence of operations) caused a loss of precision.
"""
! default = 0
class Subnormal(DecimalException):
--- 309,313 ----
operation (or sequence of operations) caused a loss of precision.
"""
! pass
class Subnormal(DecimalException):
***************
*** 383,399 ****
"""
! def _filterfunc(obj):
! """Returns true if a subclass of DecimalException"""
! try:
! return issubclass(obj, DecimalException)
! except TypeError:
! return False
!
! #Signals holds the exceptions
! Signals = filter(_filterfunc, globals().values())
!
! del _filterfunc
!
##### Context Functions #######################################
--- 376,388 ----
"""
+ # List of public traps and flags
+ Signals = [Clamped, DivisionByZero, Inexact, Overflow, Rounded,
+ Underflow, InvalidOperation, Subnormal]
! # Map conditions (per the spec) to signals
! _condition_map = {ConversionSyntax:InvalidOperation,
! DivisionImpossible:InvalidOperation,
! DivisionUndefined:InvalidOperation,
! InvalidContext:InvalidOperation}
##### Context Functions #######################################
***************
*** 2169,2173 ****
__copy__ = copy
! def _raise_error(self, error, explanation = None, *args):
"""Handles an error
--- 2158,2162 ----
__copy__ = copy
! def _raise_error(self, condition, explanation = None, *args):
"""Handles an error
***************
*** 2177,2180 ****
--- 2166,2170 ----
the default value after incrementing the flag.
"""
+ error = _condition_map.get(condition, condition)
if error in self._ignored_flags:
#Don't touch the flag
***************
*** 2184,2188 ****
if not self.trap_enablers[error]:
#The errors define how to handle themselves.
! return error().handle(self, *args)
# Errors should only be risked on copies of the context
--- 2174,2178 ----
if not self.trap_enablers[error]:
#The errors define how to handle themselves.
! return condition().handle(self, *args)
# Errors should only be risked on copies of the context
***************
*** 2208,2211 ****
--- 2198,2206 ----
self._ignored_flags.remove(flag)
+ def __hash__(self):
+ """A Context cannot be hashed."""
+ # We inherit object.__hash__, so we must deny this explicitly
+ raise TypeError, "Cannot hash a Context."
+
def Etiny(self):
"""Returns Etiny (= Emin - prec + 1)"""
More information about the Python-checkins
mailing list