Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 76e1217

Browse files
author
Stefan Krah
committed
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).
1 parent f47d79f commit 76e1217

3 files changed

Lines changed: 21 additions & 7 deletions

File tree

Lib/test/test_decimal.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,7 +1992,8 @@ def test_as_tuple(self):
19921992
d = Decimal("-4.34913534E-17")
19931993
self.assertEqual(d.as_tuple(), (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
19941994

1995-
# XXX non-compliant infinity payload.
1995+
# The '0' coefficient is implementation specific to decimal.py.
1996+
# It has no meaning in the C-version and is ignored there.
19961997
d = Decimal("Infinity")
19971998
self.assertEqual(d.as_tuple(), (0, (0,), 'F') )
19981999

@@ -2012,12 +2013,14 @@ def test_as_tuple(self):
20122013
d = Decimal( (1, (), 'n') )
20132014
self.assertEqual(d.as_tuple(), (1, (), 'n') )
20142015

2015-
# XXX coefficient in infinity should raise an error
2016-
if self.decimal == P:
2017-
d = Decimal( (0, (4, 5, 3, 4), 'F') )
2018-
self.assertEqual(d.as_tuple(), (0, (0,), 'F'))
2019-
d = Decimal( (1, (0, 2, 7, 1), 'F') )
2020-
self.assertEqual(d.as_tuple(), (1, (0,), 'F'))
2016+
# For infinities, decimal.py has always silently accepted any
2017+
# coefficient tuple.
2018+
d = Decimal( (0, (0,), 'F') )
2019+
self.assertEqual(d.as_tuple(), (0, (0,), 'F'))
2020+
d = Decimal( (0, (4, 5, 3, 4), 'F') )
2021+
self.assertEqual(d.as_tuple(), (0, (0,), 'F'))
2022+
d = Decimal( (1, (0, 2, 7, 1), 'F') )
2023+
self.assertEqual(d.as_tuple(), (1, (0,), 'F'))
20212024

20222025
def test_subclassing(self):
20232026
# Different behaviours when subclassing Decimal

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ Core and Builtins
2121
Library
2222
-------
2323

24+
- Issue #15882: Change _decimal to accept any coefficient tuple when
25+
constructing infinities. This is done for backwards compatibility
26+
with decimal.py: Infinity coefficients are undefined in _decimal
27+
(in accordance with the specification).
2428

2529
- Issue #15876: Fix a refleak in the curses module: window.encoding.
2630

Modules/_decimal/_decimal.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2364,6 +2364,7 @@ dectuple_as_str(PyObject *dectuple)
23642364
long sign, l;
23652365
mpd_ssize_t exp = 0;
23662366
Py_ssize_t i, mem, tsize;
2367+
int is_infinite = 0;
23672368
int n;
23682369

23692370
assert(PyTuple_Check(dectuple));
@@ -2399,6 +2400,7 @@ dectuple_as_str(PyObject *dectuple)
23992400
/* special */
24002401
if (PyUnicode_CompareWithASCIIString(tmp, "F") == 0) {
24012402
strcat(sign_special, "Inf");
2403+
is_infinite = 1;
24022404
}
24032405
else if (PyUnicode_CompareWithASCIIString(tmp, "n") == 0) {
24042406
strcat(sign_special, "NaN");
@@ -2470,6 +2472,11 @@ dectuple_as_str(PyObject *dectuple)
24702472
"coefficient must be a tuple of digits");
24712473
goto error;
24722474
}
2475+
if (is_infinite) {
2476+
/* accept but ignore any well-formed coefficient for compatibility
2477+
with decimal.py */
2478+
continue;
2479+
}
24732480
*cp++ = (char)l + '0';
24742481
}
24752482
*cp = '\0';

0 commit comments

Comments
 (0)