[pypy-commit] pypy decimal-libmpdec: Decimal.real and .imag

amauryfa noreply at buildbot.pypy.org
Sun Oct 5 20:23:09 CEST 2014


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: decimal-libmpdec
Changeset: r73804:18a9e5d3a851
Date: 2014-09-28 17:04 +0200
http://bitbucket.org/pypy/pypy/changeset/18a9e5d3a851/

Log:	Decimal.real and .imag

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,14 @@
             ret = ret.replace('\xff', '\0')
         return space.wrap(ret.decode('utf-8'))
 
+    def get_real(self, space):
+        context = interp_context.getcontext(space)
+        return decimal_from_decimal(space, None, self, context, exact=True)
+
+    def get_imag(self, space):
+        context = interp_context.getcontext(space)
+        return decimal_from_ssize(space, None, 0, context, exact=True)
+
     def compare(self, space, w_other, op):
         context = interp_context.getcontext(space)
         w_err, w_self, w_other = convert_binop_cmp(
@@ -1100,6 +1108,9 @@
     __round__ = interp2app(W_Decimal.descr_round),
     __format__ = interp2app(W_Decimal.descr_format),
     #
+    real = GetSetProperty(W_Decimal.get_real),
+    imag = GetSetProperty(W_Decimal.get_imag),
+    #
     __eq__ = interp2app(W_Decimal.descr_eq),
     __ne__ = interp2app(W_Decimal.descr_ne),
     __le__ = interp2app(W_Decimal.descr_le),
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
@@ -330,6 +330,12 @@
         assert str(nc.create_decimal(Decimal('NaN12345'))) == 'NaN'
         assert nc.flags[InvalidOperation]
 
+    def test_complex(self):
+        Decimal = self.decimal.Decimal
+        d = Decimal("2.34")
+        assert d.real == d
+        assert d.imag == 0
+
     def test_operations(self):
         Decimal = self.decimal.Decimal
 


More information about the pypy-commit mailing list