]> granicus.if.org Git - python/commitdiff
Issue #15882: Change _decimal to accept any coefficient tuple when
authorStefan Krah <skrah@bytereef.org>
Mon, 10 Sep 2012 17:34:58 +0000 (19:34 +0200)
committerStefan Krah <skrah@bytereef.org>
Mon, 10 Sep 2012 17:34:58 +0000 (19:34 +0200)
constructing infinities. This is done for backwards compatibility
with decimal.py: Infinity coefficients are undefined in _decimal
(in accordance with the specification).

Lib/test/test_decimal.py
Misc/NEWS
Modules/_decimal/_decimal.c

index d00ed5aa40c75b116c4e0d2bbd63bfccc459314e..3ca5927f79e9a665a1de8c561590b514e15eaba7 100644 (file)
@@ -1992,7 +1992,8 @@ class UsabilityTest(unittest.TestCase):
         d = Decimal("-4.34913534E-17")
         self.assertEqual(d.as_tuple(), (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
 
-        # XXX non-compliant infinity payload.
+        # The '0' coefficient is implementation specific to decimal.py.
+        # It has no meaning in the C-version and is ignored there.
         d = Decimal("Infinity")
         self.assertEqual(d.as_tuple(), (0, (0,), 'F') )
 
@@ -2012,12 +2013,14 @@ class UsabilityTest(unittest.TestCase):
         d = Decimal( (1, (), 'n') )
         self.assertEqual(d.as_tuple(), (1, (), 'n') )
 
-        # XXX coefficient in infinity should raise an error
-        if self.decimal == P:
-            d = Decimal( (0, (4, 5, 3, 4), 'F') )
-            self.assertEqual(d.as_tuple(), (0, (0,), 'F'))
-            d = Decimal( (1, (0, 2, 7, 1), 'F') )
-            self.assertEqual(d.as_tuple(), (1, (0,), 'F'))
+        # For infinities, decimal.py has always silently accepted any
+        # coefficient tuple.
+        d = Decimal( (0, (0,), 'F') )
+        self.assertEqual(d.as_tuple(), (0, (0,), 'F'))
+        d = Decimal( (0, (4, 5, 3, 4), 'F') )
+        self.assertEqual(d.as_tuple(), (0, (0,), 'F'))
+        d = Decimal( (1, (0, 2, 7, 1), 'F') )
+        self.assertEqual(d.as_tuple(), (1, (0,), 'F'))
 
     def test_subclassing(self):
         # Different behaviours when subclassing Decimal
index 3bc05348b6eea8bc24bd348d6b2dedab30c8615b..de603c49100bf020c6ef1e64dd7f892605ebba1e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #15882: Change _decimal to accept any coefficient tuple when
+  constructing infinities. This is done for backwards compatibility
+  with decimal.py: Infinity coefficients are undefined in _decimal
+  (in accordance with the specification).
 
 - Issue #15876: Fix a refleak in the curses module: window.encoding.
 
index 15c64e215d748fb98e3d7a3de5e2e5fb3fe331ab..996f9da17bedf6edbdebe162edfb0a517305f1ee 100644 (file)
@@ -2364,6 +2364,7 @@ dectuple_as_str(PyObject *dectuple)
     long sign, l;
     mpd_ssize_t exp = 0;
     Py_ssize_t i, mem, tsize;
+    int is_infinite = 0;
     int n;
 
     assert(PyTuple_Check(dectuple));
@@ -2399,6 +2400,7 @@ dectuple_as_str(PyObject *dectuple)
         /* special */
         if (PyUnicode_CompareWithASCIIString(tmp, "F") == 0) {
             strcat(sign_special, "Inf");
+            is_infinite = 1;
         }
         else if (PyUnicode_CompareWithASCIIString(tmp, "n") == 0) {
             strcat(sign_special, "NaN");
@@ -2470,6 +2472,11 @@ dectuple_as_str(PyObject *dectuple)
                 "coefficient must be a tuple of digits");
             goto error;
         }
+        if (is_infinite) {
+            /* accept but ignore any well-formed coefficient for compatibility
+               with decimal.py */
+            continue;
+        }
         *cp++ = (char)l + '0';
     }
     *cp = '\0';