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

Skip to content

Commit eb8c451

Browse files
author
Stefan Krah
committed
Since the return type of format() is not a Decimal, raise ValueError instead of
InvalidOperation if the format specification (width, prec) exceeds the internal limits of libmpdec.
1 parent 33f7cdd commit eb8c451

2 files changed

Lines changed: 11 additions & 11 deletions

File tree

Lib/test/test_decimal.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4971,22 +4971,16 @@ def test_c_round(self):
49714971
def test_c_format(self):
49724972
# Restricted input
49734973
Decimal = C.Decimal
4974-
InvalidOperation = C.InvalidOperation
4975-
Rounded = C.Rounded
4976-
localcontext = C.localcontext
49774974
HAVE_CONFIG_64 = (C.MAX_PREC > 425000000)
49784975

49794976
self.assertRaises(TypeError, Decimal(1).__format__, "=10.10", [], 9)
49804977
self.assertRaises(TypeError, Decimal(1).__format__, "=10.10", 9)
49814978
self.assertRaises(TypeError, Decimal(1).__format__, [])
49824979

4983-
with localcontext() as c:
4984-
c.traps[InvalidOperation] = True
4985-
c.traps[Rounded] = True
4986-
self.assertRaises(ValueError, Decimal(1).__format__, "<>=10.10")
4987-
maxsize = 2**63-1 if HAVE_CONFIG_64 else 2**31-1
4988-
self.assertRaises(InvalidOperation, Decimal("1.23456789").__format__,
4989-
"=%d.1" % maxsize)
4980+
self.assertRaises(ValueError, Decimal(1).__format__, "<>=10.10")
4981+
maxsize = 2**63-1 if HAVE_CONFIG_64 else 2**31-1
4982+
self.assertRaises(ValueError, Decimal("1.23456789").__format__,
4983+
"=%d.1" % maxsize)
49904984

49914985
def test_c_integral(self):
49924986
Decimal = C.Decimal

Modules/_decimal/_decimal.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3222,7 +3222,13 @@ dec_format(PyObject *dec, PyObject *args)
32223222

32233223
decstring = mpd_qformat_spec(MPD(dec), &spec, CTX(context), &status);
32243224
if (decstring == NULL) {
3225-
dec_addstatus(context, status);
3225+
if (status & MPD_Malloc_error) {
3226+
PyErr_NoMemory();
3227+
}
3228+
else {
3229+
PyErr_SetString(PyExc_ValueError,
3230+
"format specification exceeds internal limits of _decimal");
3231+
}
32263232
goto finish;
32273233
}
32283234
result = PyUnicode_DecodeUTF8(decstring, strlen(decstring), NULL);

0 commit comments

Comments
 (0)