Author: Amaury Forgeot d'Arc <[email protected]>
Branch: decimal-libmpdec
Changeset: r73809:5cbebee1fe07
Date: 2014-09-30 23:17 +0200
http://bitbucket.org/pypy/pypy/changeset/5cbebee1fe07/

Log:    Fix, and add pickle support.

diff --git a/pypy/module/_decimal/interp_context.py 
b/pypy/module/_decimal/interp_context.py
--- a/pypy/module/_decimal/interp_context.py
+++ b/pypy/module/_decimal/interp_context.py
@@ -178,7 +178,7 @@
                         "valid range for prec is [1, MAX_PREC]")
 
     def get_rounding(self, space):
-        return space.wrap(rmpdec.mpd_getround(self.ctx))
+        return space.wrap(self._rounding_string(space))
 
     def set_rounding(self, space, w_rounding):
         rounding = space.str_w(w_rounding)
@@ -387,7 +387,7 @@
     return interp2app(func_w)
 
 W_Context.typedef = TypeDef(
-    'Context',
+    '_decimal.Context',
     __new__ = interp2app(descr_new_context),
     __init__ = interp2app(W_Context.descr_init),
     # Attributes
diff --git a/pypy/module/_decimal/interp_decimal.py 
b/pypy/module/_decimal/interp_decimal.py
--- a/pypy/module/_decimal/interp_decimal.py
+++ b/pypy/module/_decimal/interp_decimal.py
@@ -360,6 +360,10 @@
             ret = ret.replace('\xff', '\0')
         return space.wrap(ret.decode('utf-8'))
 
+    def descr_reduce(self, space):
+        return space.newtuple([space.type(self),
+                               space.newtuple([space.str(self)])])
+
     def get_real(self, space):
         context = interp_context.getcontext(space)
         return decimal_from_decimal(space, None, self, context, exact=True)
@@ -1111,7 +1115,7 @@
                                exact=True)
 
 W_Decimal.typedef = TypeDef(
-    'Decimal',
+    '_decimal.Decimal',
     __new__ = interp2app(descr_new_decimal),
     __str__ = interp2app(W_Decimal.descr_str),
     __repr__ = interp2app(W_Decimal.descr_repr),
@@ -1124,6 +1128,7 @@
     __ceil__ = interp2app(W_Decimal.descr_ceil),
     __round__ = interp2app(W_Decimal.descr_round),
     __format__ = interp2app(W_Decimal.descr_format),
+    __reduce__ = interp2app(W_Decimal.descr_reduce),
     #
     real = GetSetProperty(W_Decimal.get_real),
     imag = GetSetProperty(W_Decimal.get_imag),
diff --git a/pypy/module/_decimal/test/test_context.py 
b/pypy/module/_decimal/test/test_context.py
--- a/pypy/module/_decimal/test/test_context.py
+++ b/pypy/module/_decimal/test/test_context.py
@@ -43,6 +43,31 @@
         nc = copy.deepcopy(c)
         assert nc.traps[self.decimal.InvalidOperation] == False
 
+    def test_none_args(self):
+        Context = self.decimal.Context
+        InvalidOperation = self.decimal.InvalidOperation
+        DivisionByZero = self.decimal.DivisionByZero
+        Overflow = self.decimal.Overflow
+
+        def assert_signals(context, attr, expected):
+            d = getattr(context, attr)
+            self.assertTrue(
+                all(d[s] if s in expected else not d[s] for s in d))
+
+        c1 = Context()
+        c2 = Context(prec=None, rounding=None, Emax=None, Emin=None,
+                     capitals=None, clamp=None, flags=None, traps=None)
+        for c in [c1, c2]:
+            self.assertEqual(c.prec, 28)
+            self.assertEqual(c.rounding, self.decimal.ROUND_HALF_EVEN)
+            self.assertEqual(c.Emax, 999999)
+            self.assertEqual(c.Emin, -999999)
+            self.assertEqual(c.capitals, 1)
+            self.assertEqual(c.clamp, 0)
+            assert_signals(c, 'flags', [])
+            assert_signals(c, 'traps', [InvalidOperation, DivisionByZero,
+                                        Overflow])
+
     def test_context_repr(self):
         c = self.decimal.DefaultContext.copy()
 
@@ -374,3 +399,19 @@
             z = y.copy_sign(Decimal(1))
             self.assertEqual(z, x)
 
+    def test_pickle(self):
+        import pickle
+        Context = self.decimal.Context
+
+        # Round trip
+        c = Context()
+        e = pickle.loads(pickle.dumps(c))
+
+        self.assertEqual(c.prec, e.prec)
+        self.assertEqual(c.Emin, e.Emin)
+        self.assertEqual(c.Emax, e.Emax)
+        self.assertEqual(c.rounding, e.rounding)
+        self.assertEqual(c.capitals, e.capitals)
+        self.assertEqual(c.clamp, e.clamp)
+        self.assertEqual(c.flags, e.flags)
+        self.assertEqual(c.traps, e.traps)
diff --git a/pypy/module/_decimal/test/test_decimal.py 
b/pypy/module/_decimal/test/test_decimal.py
--- a/pypy/module/_decimal/test/test_decimal.py
+++ b/pypy/module/_decimal/test/test_decimal.py
@@ -1343,3 +1343,10 @@
         self.assertEqual(get_fmt(Decimal('-1.5'), dotsep_wide, '020n'),
                          '-0\u00b4000\u00b4000\u00b4000\u00b4001\u00bf5')
 
+    def test_pickle(self):
+        import pickle
+        Decimal = self.decimal.Decimal
+        d = Decimal('-3.141590000')
+        p = pickle.dumps(d)
+        e = pickle.loads(p)
+        self.assertEqual(d, e)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to