[Python-checkins] python/dist/src/Lib decimal.py,1.2,1.3

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Sat Jul 3 06:02:30 EDT 2004


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11112

Modified Files:
	decimal.py 
Log Message:
Work through several open todos:
* Added test for pickling contexts
* Renamed ExceptionList to Signals (to match wording in the spec)
* Simplified Context constructor by allowing flags=None to automatically
  generate a zeroed-out flags dictionary.
* inlined _convertString() which was used only once
* _rounding_decision is private, so excluded its contants from __all__.
* added an XXX comment with concerns about subclassing signals results in
  a deviation from the spec (maybe important, maybe not).
* Taught the test_suite to determine its own directory (modeled after code
  in regrtest.py).  Enables it to be run when the current directory is not
  the test directory.
* Added a clear_flags() method to the Context API to make it easier to do
  a common operation with flags.
* Fixed the trap_enablers defaults in BasicDefaultContext to match the spec.



Index: decimal.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/decimal.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** decimal.py	1 Jul 2004 14:28:36 -0000	1.2
--- decimal.py	3 Jul 2004 10:02:27 -0000	1.3
***************
*** 10,20 ****
  
  # Todo:
- #    Add deepcopy and pickle support for contexts
- #    Consider having a SimpleDecimal subclass implementing X3.274 semantics
- #    Improve the Context API
- #         Especially with respect to setting flags and traps
- #         Consider adding a clear_flags() method to Context
  #    Provide a clean way of attaching monetary format representations
! #    Review all exposed constants for utility vs. namespace clutter
  
  
--- 10,15 ----
  
  # Todo:
  #    Provide a clean way of attaching monetary format representations
! #    Make tests independent of DefaultContext.prec == 9
  
  
***************
*** 140,145 ****
      'ROUND_DOWN', 'ROUND_HALF_UP', 'ROUND_HALF_EVEN', 'ROUND_CEILING',
      'ROUND_FLOOR', 'ROUND_UP', 'ROUND_HALF_DOWN',
!     'NEVER_ROUND', 'ALWAYS_ROUND',
!     'ExceptionList',    # <-- Used for building trap/flag dictionaries
  
      # Functions for manipulating contexts
--- 135,139 ----
      'ROUND_DOWN', 'ROUND_HALF_UP', 'ROUND_HALF_EVEN', 'ROUND_CEILING',
      'ROUND_FLOOR', 'ROUND_UP', 'ROUND_HALF_DOWN',
!     'Signals',    # <-- Used for building trap/flag dictionaries
  
      # Functions for manipulating contexts
***************
*** 245,248 ****
--- 239,248 ----
          return NaN
  
+ # XXX Is there a logic error in subclassing InvalidOperation?
+ # Setting the InvalidOperation trap to zero does not preclude ConversionSyntax.
+ # Also, incrementing Conversion syntax flag will not increment InvalidOperation.
+ # Both of these issues interfere with cross-language portability because
+ # code following the spec would not know about the Python subclasses.
+ 
  class ConversionSyntax(InvalidOperation):
      """Trying to convert badly formed string.
***************
*** 411,416 ****
          return False
  
! #ExceptionList holds the exceptions
! ExceptionList = filter(_filterfunc, globals().values())
  
  del _filterfunc
--- 411,416 ----
          return False
  
! #Signals holds the exceptions
! Signals = filter(_filterfunc, globals().values())
  
  del _filterfunc
***************
*** 493,497 ****
                  self._int = tuple(map(int, diag)) #Diagnostic info
                  return
!             self._convertString(value, context)
              return
  
--- 493,500 ----
                  self._int = tuple(map(int, diag)) #Diagnostic info
                  return
!             try:
!                 self._sign, self._int, self._exp = _string2exact(value)
!             except ValueError:
!                 self._sign, self._int, self._exp = context._raise_error(ConversionSyntax)
              return
  
***************
*** 595,611 ****
          return 0
  
-     def _convertString(self, value, context=None):
-         """Changes self's value to that in a string.
- 
-         A bad string causes a ConversionSyntax error.
-         """
-         if context is None:
-             context = getcontext()
-         try:
-             self._sign, self._int, self._exp = _string2exact(value)
-         except ValueError:
-             self._sign, self._int, self._exp = context._raise_error(ConversionSyntax)
-         return
- 
      def __nonzero__(self):
          """Is the number non-zero?
--- 598,601 ----
***************
*** 1434,1439 ****
      def __int__(self):
          """Converts self to a int, truncating if necessary."""
-         # XXX This should be implemented in terms of tested
-         # functions in the standard
          if self._isnan():
              context = getcontext()
--- 1424,1427 ----
***************
*** 2116,2119 ****
--- 2104,2109 ----
          return self.__class__(str(self))
  
+ ##### Context class ###########################################
+ 
  
  # get rounding method function:
***************
*** 2153,2156 ****
--- 2143,2148 ----
                   capitals=1, _clamp=0,
                   _ignored_flags=[]):
+         if flags is None:
+             flags = dict.fromkeys(Signals, 0)
          DefaultLock.acquire()
          for name, val in locals().items():
***************
*** 2162,2165 ****
--- 2154,2162 ----
          del self.self
  
+     def clear_flags(self):
+         """Reset all flags to zero"""
+         for flag in self.flags:
+             self.flag = 0
+ 
      def copy(self):
          """Returns a copy from self."""
***************
*** 2168,2172 ****
                           self.capitals, self._clamp, self._ignored_flags)
          return nc
-     __copy__ = copy
  
      def _raise_error(self, error, explanation = None, *args):
--- 2165,2168 ----
***************
*** 2193,2197 ****
      def _ignore_all_flags(self):
          """Ignore all flags, if they are raised"""
!         return self._ignore_flags(*ExceptionList)
  
      def _ignore_flags(self, *flags):
--- 2189,2193 ----
      def _ignore_all_flags(self):
          """Ignore all flags, if they are raised"""
!         return self._ignore_flags(*Signals)
  
      def _ignore_flags(self, *flags):
***************
*** 2960,2969 ****
  ##### Setup Specific Contexts ################################
  
! def _zero_exceptions():
!     "Helper function mapping all exceptions to zero."
!     d = {}
!     for exception in ExceptionList:
!         d[exception] = 0
!     return d
  
  # The default context prototype used by Context()
--- 2956,2961 ----
  ##### Setup Specific Contexts ################################
  
! _basic_traps = dict.fromkeys(Signals, 1)
! _basic_traps.update({Inexact:0, Rounded:0, Subnormal:0})
  
  # The default context prototype used by Context()
***************
*** 2972,2977 ****
  DefaultContext = Context(
          prec=SINGLE_PRECISION, rounding=ROUND_HALF_EVEN,
!         trap_enablers=_zero_exceptions(),
!         flags=_zero_exceptions(),
          _rounding_decision=ALWAYS_ROUND,
  )
--- 2964,2969 ----
  DefaultContext = Context(
          prec=SINGLE_PRECISION, rounding=ROUND_HALF_EVEN,
!         trap_enablers=dict.fromkeys(Signals, 0),
!         flags=None,
          _rounding_decision=ALWAYS_ROUND,
  )
***************
*** 2982,2992 ****
  # of the spec.
  
- _basic_traps = _zero_exceptions()
- _basic_traps.update({Inexact:1, Rounded:1, Subnormal:1})
- 
  BasicDefaultContext = Context(
          prec=9, rounding=ROUND_HALF_UP,
          trap_enablers=_basic_traps,
!         flags=_zero_exceptions(),
          _rounding_decision=ALWAYS_ROUND,
  )
--- 2974,2981 ----
  # of the spec.
  
  BasicDefaultContext = Context(
          prec=9, rounding=ROUND_HALF_UP,
          trap_enablers=_basic_traps,
!         flags=None,
          _rounding_decision=ALWAYS_ROUND,
  )
***************
*** 2994,3004 ****
  ExtendedDefaultContext = Context(
          prec=SINGLE_PRECISION, rounding=ROUND_HALF_EVEN,
!         trap_enablers=_zero_exceptions(),
!         flags=_zero_exceptions(),
          _rounding_decision=ALWAYS_ROUND,
  )
  
  
! ##### Useful Constants (internal use only######################
  
  #Reusable defaults
--- 2983,2993 ----
  ExtendedDefaultContext = Context(
          prec=SINGLE_PRECISION, rounding=ROUND_HALF_EVEN,
!         trap_enablers=dict.fromkeys(Signals, 0),
!         flags=None,
          _rounding_decision=ALWAYS_ROUND,
  )
  
  
! ##### Useful Constants (internal use only) ####################
  
  #Reusable defaults




More information about the Python-checkins mailing list