Author: Amaury Forgeot d'Arc <[email protected]>
Branch: decimal-libmpdec
Changeset: r71548:3fd3d455666c
Date: 2014-05-11 14:36 +0200
http://bitbucket.org/pypy/pypy/changeset/3fd3d455666c/

Log:    Add Context.add()

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
@@ -210,6 +210,19 @@
                 self.capitals, rffi.cast(lltype.Signed, self.ctx.c_clamp),
                 flags, traps))
 
+    # Binary arithmetic functions
+    def binary_method(self, space, mpd_func, w_x, w_y):
+        from pypy.module._decimal.interp_decimal import W_Decimal
+        w_a, w_b = W_Decimal.convert_binop_raise(space, self, w_x, w_y)
+        w_result = W_Decimal.allocate(space)
+        with self.catch_status(space) as (ctx, status_ptr):
+            mpd_func(w_result.mpd, w_a.mpd, w_b.mpd, ctx, status_ptr)
+        return w_result
+
+    def add_w(self, space, w_x, w_y):
+        return self.binary_method(space, rmpdec.mpd_qadd, w_x, w_y)
+        
+
 
 def descr_new_context(space, w_subtype, __args__):
     w_result = space.allocate_instance(W_Context, w_subtype)
@@ -235,6 +248,8 @@
     clear_flags=interp2app(W_Context.clear_flags_w),
     clear_traps=interp2app(W_Context.clear_traps_w),
     create_decimal=interp2app(W_Context.create_decimal_w),
+    # Operations
+    add=interp2app(W_Context.add_w),
     )
 
 
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
@@ -175,7 +175,7 @@
 
     # Operations
     @staticmethod
-    def convert_op(space, w_value, context):
+    def convert_op(space, context, w_value):
         if isinstance(w_value, W_Decimal):
             return None, w_value
         elif space.isinstance_w(w_value, space.w_int):
@@ -184,19 +184,34 @@
                                              exact=True)
         return space.w_NotImplemented, None
 
-    def convert_binop(self, space, w_other, context):
-        w_err, w_a = W_Decimal.convert_op(space, self, context)
+    @staticmethod
+    def convert_binop(space, context, w_x, w_y):
+        w_err, w_a = W_Decimal.convert_op(space, context, w_x)
         if w_err:
             return w_err, None, None
-        w_err, w_b = W_Decimal.convert_op(space, w_other, context)
+        w_err, w_b = W_Decimal.convert_op(space, context, w_y)
         if w_err:
             return w_err, None, None
         return None, w_a, w_b
 
+    @staticmethod
+    def convert_binop_raise(space, context, w_x, w_y):
+        w_err, w_a = W_Decimal.convert_op(space, context, w_x)
+        if w_err:
+            raise oefmt(space.w_TypeError,
+                        "conversion from %N to Decimal is not supported",
+                        space.type(w_x))
+        w_err, w_b = W_Decimal.convert_op(space, context, w_y)
+        if w_err:
+            raise oefmt(space.w_TypeError,
+                        "conversion from %N to Decimal is not supported",
+                        space.type(w_y))
+        return w_a, w_b
+
     def binary_number_method(self, space, mpd_func, w_other):
         context = interp_context.getcontext(space)
 
-        w_err, w_a, w_b = self.convert_binop(space, w_other, context)
+        w_err, w_a, w_b = W_Decimal.convert_binop(space, context, self, 
w_other)
         if w_err:
             return w_err
         w_result = W_Decimal.allocate(space)
@@ -279,7 +294,7 @@
 
     # sign
     try:
-        sign = space.int_w(w_sign)
+        sign = space.int_w(w_sign, allow_conversion=False)
     except OperationError as e:
         if not e.match(space, space.w_TypeError):
             raise
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
@@ -437,3 +437,17 @@
         for d, n, r in test_triples:
             assert str(round(Decimal(d), n)) == r
 
+    def test_add(self):
+        Decimal = self.decimal.Decimal
+        Context = self.decimal.Context
+
+        c = Context()
+        d = c.add(Decimal(1), Decimal(1))
+        assert c.add(1, 1) == d
+        assert c.add(Decimal(1), 1) == d
+        assert c.add(1, Decimal(1)) == d
+        raises(TypeError, c.add, '1', 1)
+        raises(TypeError, c.add, 1, '1')
+
+    
+
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to