Commits

Amaury Forgeot d'Arc  committed 2ca4490

Add decimal "Signals", i.e. exceptions.

  • Participants
  • Parent commits a858f7d
  • Branches decimal-libmpdec

Comments (0)

Files changed (3)

File pypy/module/_decimal/__init__.py

     interpleveldefs = {
         'Decimal': 'interp_decimal.W_Decimal',
         'getcontext': 'interp_context.getcontext',
+
         'IEEE_CONTEXT_MAX_BITS': 'space.wrap(interp_decimal.IEEE_CONTEXT_MAX_BITS)',
         }
+    for name in ('DecimalException', 'Clamped', 'Rounded', 'Inexact',
+                 'Subnormal', 'Underflow', 'Overflow', 'DivisionByZero',
+                 'InvalidOperation', 'FloatOperation'):
+        interpleveldefs[name] = 'interp_signals.get(space).w_%s' % name
+        

File pypy/module/_decimal/interp_signals.py

+class SignalState:
+    def __init__(self, space):
+        self.w_DecimalException = space.call_function(
+            space.w_type, space.wrap("DecimalException"),
+            space.newtuple([space.w_ArithmeticError]),
+            space.newdict())
+        self.w_Clamped = space.call_function(
+            space.w_type, space.wrap("Clamped"),
+            space.newtuple([self.w_DecimalException]),
+            space.newdict())
+        self.w_Rounded = space.call_function(
+            space.w_type, space.wrap("Rounded"),
+            space.newtuple([self.w_DecimalException]),
+            space.newdict())
+        self.w_Inexact = space.call_function(
+            space.w_type, space.wrap("Inexact"),
+            space.newtuple([self.w_DecimalException]),
+            space.newdict())
+        self.w_Subnormal = space.call_function(
+            space.w_type, space.wrap("Subnormal"),
+            space.newtuple([self.w_DecimalException]),
+            space.newdict())
+        self.w_Underflow = space.call_function(
+            space.w_type, space.wrap("Underflow"),
+            space.newtuple([self.w_Inexact,
+                            self.w_Rounded,
+                            self.w_Subnormal]),
+            space.newdict())
+        self.w_Overflow = space.call_function(
+            space.w_type, space.wrap("Overflow"),
+            space.newtuple([self.w_Inexact,
+                            self.w_Rounded]),
+            space.newdict())
+        self.w_DivisionByZero = space.call_function(
+            space.w_type, space.wrap("DivisionByZero"),
+            space.newtuple([self.w_DecimalException,
+                            space.w_ZeroDivisionError]),
+            space.newdict())
+        self.w_InvalidOperation = space.call_function(
+            space.w_type, space.wrap("InvalidOperation"),
+            space.newtuple([self.w_DecimalException]),
+            space.newdict())
+        self.w_FloatOperation = space.call_function(
+            space.w_type, space.wrap("FloatOperation"),
+            space.newtuple([self.w_DecimalException,
+                            space.w_TypeError]),
+            space.newdict())
+
+def get(space):
+    return space.fromcache(SignalState)

File pypy/module/_decimal/test/test_module.py

         assert type(flags).__name__ == 'SignalDict'
         bases = type(flags).__bases__
         assert bases[1] is MutableMapping
+
+    def test_exceptions(self):
+        import _decimal
+        for name in ('Clamped', 'Rounded', 'Inexact', 'Subnormal',
+                     'Underflow', 'Overflow', 'DivisionByZero',
+                     'InvalidOperation', 'FloatOperation'):
+            ex = getattr(_decimal, name)
+            assert issubclass(ex, _decimal.DecimalException)
+            assert issubclass(ex, ArithmeticError)
+