[Python-checkins] cpython: Allow printing a leading '-' and the maximum number of exponent digits
stefan.krah
python-checkins at python.org
Thu Apr 5 16:27:27 CEST 2012
http://hg.python.org/cpython/rev/a99cd3b98425
changeset: 76121:a99cd3b98425
user: Stefan Krah <skrah at bytereef.org>
date: Thu Apr 05 15:46:19 2012 +0200
summary:
Allow printing a leading '-' and the maximum number of exponent digits
rather than raising RuntimeError (allocated space is sufficient for the
additional character).
files:
Lib/test/test_decimal.py | 48 +++++++++++++++++++++++++
Modules/_decimal/_decimal.c | 4 +-
2 files changed, 50 insertions(+), 2 deletions(-)
diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -4977,6 +4977,54 @@
x = "1e%d" % (-sys.maxsize-1)
self.assertRaises(InvalidOperation, Decimal, x)
+ def test_from_tuple(self):
+ Decimal = C.Decimal
+ localcontext = C.localcontext
+ InvalidOperation = C.InvalidOperation
+ Overflow = C.Overflow
+ Underflow = C.Underflow
+
+ with localcontext() as c:
+
+ c.traps[InvalidOperation] = True
+ c.traps[Overflow] = True
+ c.traps[Underflow] = True
+
+ # SSIZE_MAX
+ x = (1, (), sys.maxsize)
+ self.assertEqual(str(c.create_decimal(x)), '-0E+999999')
+ self.assertRaises(InvalidOperation, Decimal, x)
+
+ x = (1, (0, 1, 2), sys.maxsize)
+ self.assertRaises(Overflow, c.create_decimal, x)
+ self.assertRaises(InvalidOperation, Decimal, x)
+
+ # SSIZE_MIN
+ x = (1, (), -sys.maxsize-1)
+ self.assertEqual(str(c.create_decimal(x)), '-0E-1000026')
+ self.assertRaises(InvalidOperation, Decimal, x)
+
+ x = (1, (0, 1, 2), -sys.maxsize-1)
+ self.assertRaises(Underflow, c.create_decimal, x)
+ self.assertRaises(InvalidOperation, Decimal, x)
+
+ # OverflowError
+ x = (1, (), sys.maxsize+1)
+ self.assertRaises(OverflowError, c.create_decimal, x)
+ self.assertRaises(OverflowError, Decimal, x)
+
+ x = (1, (), -sys.maxsize-2)
+ self.assertRaises(OverflowError, c.create_decimal, x)
+ self.assertRaises(OverflowError, Decimal, x)
+
+ # Specials
+ x = (1, (), "N")
+ self.assertEqual(str(Decimal(x)), '-sNaN')
+ x = (1, (0,), "N")
+ self.assertEqual(str(Decimal(x)), '-sNaN')
+ x = (1, (0, 1), "N")
+ self.assertEqual(str(Decimal(x)), '-sNaN1')
+
all_tests = [
CExplicitConstructionTest, PyExplicitConstructionTest,
diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c
--- a/Modules/_decimal/_decimal.c
+++ b/Modules/_decimal/_decimal.c
@@ -2435,8 +2435,8 @@
if (sign_special[1] == '\0') {
/* not a special number */
*cp++ = 'E';
- n = snprintf(cp, MPD_EXPDIGITS+1, "%" PRI_mpd_ssize_t, exp);
- if (n < 0 || n >= MPD_EXPDIGITS+1) {
+ n = snprintf(cp, MPD_EXPDIGITS+2, "%" PRI_mpd_ssize_t, exp);
+ if (n < 0 || n >= MPD_EXPDIGITS+2) {
PyErr_SetString(PyExc_RuntimeError,
"internal error in dec_sequence_as_str");
goto error;
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list