Commits

Amaury Forgeot d'Arc  committed 1df1301

Add Context.is_* methods

  • Participants
  • Parent commits bf6ff9d
  • Branches decimal-libmpdec

Comments (0)

Files changed (3)

File pypy/module/_decimal/interp_context.py

     W_Context.__init__(w_result, space)
     return w_result
 
+def make_bool_function(mpd_func):
+    @unwrap_spec(w_context=W_Context)
+    def func_w(space, w_context, w_x):
+        from pypy.module._decimal import interp_decimal
+        w_x = interp_decimal.convert_op_raise(space, w_context, w_x)
+        res = getattr(rmpdec, mpd_func)(w_x.mpd, w_context.ctx)
+        return space.wrap(bool(res))
+    return interp2app(func_w)
+
+def make_bool_function_noctx(mpd_func):
+    @unwrap_spec(w_context=W_Context)
+    def func_w(space, w_context, w_x):
+        from pypy.module._decimal import interp_decimal
+        w_x = interp_decimal.convert_op_raise(space, w_context, w_x)
+        res = getattr(rmpdec, mpd_func)(w_x.mpd)
+        return space.wrap(bool(res))
+    return interp2app(func_w)
+
 W_Context.typedef = TypeDef(
     'Context',
     __new__ = interp2app(descr_new_context),
     # Ternary operations
     power=interp2app(W_Context.power_w),
     fma=interp2app(W_Context.fma_w),
+    # Boolean operations
+    is_signed=make_bool_function_noctx('mpd_issigned'),
+    is_zero=make_bool_function_noctx('mpd_iszero'),
+    is_normal=make_bool_function('mpd_isnormal'),
+    is_subnormal=make_bool_function('mpd_issubnormal'),
+    is_finite=make_bool_function_noctx('mpd_isfinite'),
+    is_infinite=make_bool_function_noctx('mpd_isinfinite'),
+    is_nan=make_bool_function_noctx('mpd_isnan'),
+    is_qnan=make_bool_function_noctx('mpd_isqnan'),
+    is_snan=make_bool_function_noctx('mpd_issnan'),
     )
 
 

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

         self.assertRaises(TypeError, c.logical_xor, '1', 1)
         self.assertRaises(TypeError, c.logical_xor, 1, '1')
 
+    def test_is_finite(self):
+        Decimal = self.decimal.Decimal
+        Context = self.decimal.Context
+
+        c = Context()
+        d = c.is_finite(Decimal(10))
+        self.assertEqual(c.is_finite(10), d)
+        self.assertRaises(TypeError, c.is_finite, '10')
+
+    def test_is_infinite(self):
+        Decimal = self.decimal.Decimal
+        Context = self.decimal.Context
+
+        c = Context()
+        d = c.is_infinite(Decimal(10))
+        self.assertEqual(c.is_infinite(10), d)
+        self.assertRaises(TypeError, c.is_infinite, '10')
+
+    def test_is_nan(self):
+        Decimal = self.decimal.Decimal
+        Context = self.decimal.Context
+
+        c = Context()
+        d = c.is_nan(Decimal(10))
+        self.assertEqual(c.is_nan(10), d)
+        self.assertRaises(TypeError, c.is_nan, '10')
+
+    def test_is_normal(self):
+        Decimal = self.decimal.Decimal
+        Context = self.decimal.Context
+
+        c = Context()
+        d = c.is_normal(Decimal(10))
+        self.assertEqual(c.is_normal(10), d)
+        self.assertRaises(TypeError, c.is_normal, '10')
+
+    def test_is_qnan(self):
+        Decimal = self.decimal.Decimal
+        Context = self.decimal.Context
+
+        c = Context()
+        d = c.is_qnan(Decimal(10))
+        self.assertEqual(c.is_qnan(10), d)
+        self.assertRaises(TypeError, c.is_qnan, '10')
+
+    def test_is_signed(self):
+        Decimal = self.decimal.Decimal
+        Context = self.decimal.Context
+
+        c = Context()
+        d = c.is_signed(Decimal(10))
+        self.assertEqual(c.is_signed(10), d)
+        self.assertRaises(TypeError, c.is_signed, '10')
+
+    def test_is_snan(self):
+        Decimal = self.decimal.Decimal
+        Context = self.decimal.Context
+
+        c = Context()
+        d = c.is_snan(Decimal(10))
+        self.assertEqual(c.is_snan(10), d)
+        self.assertRaises(TypeError, c.is_snan, '10')
+
+    def test_is_subnormal(self):
+        Decimal = self.decimal.Decimal
+        Context = self.decimal.Context
+
+        c = Context()
+        d = c.is_subnormal(Decimal(10))
+        self.assertEqual(c.is_subnormal(10), d)
+        self.assertRaises(TypeError, c.is_subnormal, '10')
+
+    def test_is_zero(self):
+        Decimal = self.decimal.Decimal
+        Context = self.decimal.Context
+
+        c = Context()
+        d = c.is_zero(Decimal(10))
+        self.assertEqual(c.is_zero(10), d)
+        self.assertRaises(TypeError, c.is_zero, '10')
+

File rpython/rlib/rmpdec.py

         "mpd_maxcontext",
         "mpd_qnew", "mpd_del",
         "mpd_to_sci", "mpd_to_sci_size",
-        "mpd_iszero", "mpd_isnegative", "mpd_isinfinite", "mpd_isspecial",
+        "mpd_iszero", "mpd_isnegative", "mpd_issigned",
+        "mpd_isfinite", "mpd_isinfinite",
+        "mpd_isnormal", "mpd_issubnormal", "mpd_isspecial",
         "mpd_isnan", "mpd_issnan", "mpd_isqnan",
         "mpd_qcmp", "mpd_qcompare", "mpd_qcompare_signal",
         "mpd_qmin", "mpd_qmax", "mpd_qmin_mag", "mpd_qmax_mag",
     'mpd_iszero', [MPD_PTR], rffi.INT)
 mpd_isnegative = external(
     'mpd_isnegative', [MPD_PTR], rffi.INT)
+mpd_issigned = external(
+    'mpd_issigned', [MPD_PTR], rffi.INT)
+mpd_isfinite = external(
+    'mpd_isfinite', [MPD_PTR], rffi.INT)
 mpd_isinfinite = external(
     'mpd_isinfinite', [MPD_PTR], rffi.INT)
+mpd_isnormal = external(
+    'mpd_isnormal', [MPD_PTR, MPD_CONTEXT_PTR], rffi.INT)
+mpd_issubnormal = external(
+    'mpd_issubnormal', [MPD_PTR, MPD_CONTEXT_PTR], rffi.INT)
 mpd_isspecial = external(
     'mpd_isspecial', [MPD_PTR], rffi.INT)
 mpd_isnan = external(