Commit 76e12179 authored by Stefan Krah's avatar Stefan Krah

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).
parent f47d79fe
...@@ -1992,7 +1992,8 @@ class UsabilityTest(unittest.TestCase): ...@@ -1992,7 +1992,8 @@ class UsabilityTest(unittest.TestCase):
d = Decimal("-4.34913534E-17") d = Decimal("-4.34913534E-17")
self.assertEqual(d.as_tuple(), (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) ) 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") d = Decimal("Infinity")
self.assertEqual(d.as_tuple(), (0, (0,), 'F') ) self.assertEqual(d.as_tuple(), (0, (0,), 'F') )
...@@ -2012,8 +2013,10 @@ class UsabilityTest(unittest.TestCase): ...@@ -2012,8 +2013,10 @@ class UsabilityTest(unittest.TestCase):
d = Decimal( (1, (), 'n') ) d = Decimal( (1, (), 'n') )
self.assertEqual(d.as_tuple(), (1, (), 'n') ) self.assertEqual(d.as_tuple(), (1, (), 'n') )
# XXX coefficient in infinity should raise an error # For infinities, decimal.py has always silently accepted any
if self.decimal == P: # coefficient tuple.
d = Decimal( (0, (0,), 'F') )
self.assertEqual(d.as_tuple(), (0, (0,), 'F'))
d = Decimal( (0, (4, 5, 3, 4), 'F') ) d = Decimal( (0, (4, 5, 3, 4), 'F') )
self.assertEqual(d.as_tuple(), (0, (0,), 'F')) self.assertEqual(d.as_tuple(), (0, (0,), 'F'))
d = Decimal( (1, (0, 2, 7, 1), 'F') ) d = Decimal( (1, (0, 2, 7, 1), 'F') )
......
...@@ -21,6 +21,10 @@ Core and Builtins ...@@ -21,6 +21,10 @@ Core and Builtins
Library 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. - Issue #15876: Fix a refleak in the curses module: window.encoding.
......
...@@ -2364,6 +2364,7 @@ dectuple_as_str(PyObject *dectuple) ...@@ -2364,6 +2364,7 @@ dectuple_as_str(PyObject *dectuple)
long sign, l; long sign, l;
mpd_ssize_t exp = 0; mpd_ssize_t exp = 0;
Py_ssize_t i, mem, tsize; Py_ssize_t i, mem, tsize;
int is_infinite = 0;
int n; int n;
assert(PyTuple_Check(dectuple)); assert(PyTuple_Check(dectuple));
...@@ -2399,6 +2400,7 @@ dectuple_as_str(PyObject *dectuple) ...@@ -2399,6 +2400,7 @@ dectuple_as_str(PyObject *dectuple)
/* special */ /* special */
if (PyUnicode_CompareWithASCIIString(tmp, "F") == 0) { if (PyUnicode_CompareWithASCIIString(tmp, "F") == 0) {
strcat(sign_special, "Inf"); strcat(sign_special, "Inf");
is_infinite = 1;
} }
else if (PyUnicode_CompareWithASCIIString(tmp, "n") == 0) { else if (PyUnicode_CompareWithASCIIString(tmp, "n") == 0) {
strcat(sign_special, "NaN"); strcat(sign_special, "NaN");
...@@ -2470,6 +2472,11 @@ dectuple_as_str(PyObject *dectuple) ...@@ -2470,6 +2472,11 @@ dectuple_as_str(PyObject *dectuple)
"coefficient must be a tuple of digits"); "coefficient must be a tuple of digits");
goto error; goto error;
} }
if (is_infinite) {
/* accept but ignore any well-formed coefficient for compatibility
with decimal.py */
continue;
}
*cp++ = (char)l + '0'; *cp++ = (char)l + '0';
} }
*cp = '\0'; *cp = '\0';
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment