]> granicus.if.org Git - python/commitdiff
Since the return type of format() is not a Decimal, raise ValueError instead of
authorStefan Krah <skrah@bytereef.org>
Thu, 24 Jan 2013 14:22:33 +0000 (15:22 +0100)
committerStefan Krah <skrah@bytereef.org>
Thu, 24 Jan 2013 14:22:33 +0000 (15:22 +0100)
InvalidOperation if the format specification (width, prec) exceeds the internal
limits of libmpdec.

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

index 37637d66320e8cd4284937724dcc16261eb2ea68..69a2fafffb306dac3d4c0e3812c0ab64a97dfcdb 100644 (file)
@@ -4971,22 +4971,16 @@ class CWhitebox(unittest.TestCase):
     def test_c_format(self):
         # Restricted input
         Decimal = C.Decimal
-        InvalidOperation = C.InvalidOperation
-        Rounded = C.Rounded
-        localcontext = C.localcontext
         HAVE_CONFIG_64 = (C.MAX_PREC > 425000000)
 
         self.assertRaises(TypeError, Decimal(1).__format__, "=10.10", [], 9)
         self.assertRaises(TypeError, Decimal(1).__format__, "=10.10", 9)
         self.assertRaises(TypeError, Decimal(1).__format__, [])
 
-        with localcontext() as c:
-            c.traps[InvalidOperation] = True
-            c.traps[Rounded] = True
-            self.assertRaises(ValueError, Decimal(1).__format__, "<>=10.10")
-            maxsize = 2**63-1 if HAVE_CONFIG_64 else 2**31-1
-            self.assertRaises(InvalidOperation, Decimal("1.23456789").__format__,
-                              "=%d.1" % maxsize)
+        self.assertRaises(ValueError, Decimal(1).__format__, "<>=10.10")
+        maxsize = 2**63-1 if HAVE_CONFIG_64 else 2**31-1
+        self.assertRaises(ValueError, Decimal("1.23456789").__format__,
+                          "=%d.1" % maxsize)
 
     def test_c_integral(self):
         Decimal = C.Decimal
index 5e9fd6752299c0fe48aaef748495178af6b28f8d..89386ffa9b325911b7aa3c0bd5615aa389f17016 100644 (file)
@@ -3222,7 +3222,13 @@ dec_format(PyObject *dec, PyObject *args)
 
     decstring = mpd_qformat_spec(MPD(dec), &spec, CTX(context), &status);
     if (decstring == NULL) {
-        dec_addstatus(context, status);
+        if (status & MPD_Malloc_error) {
+            PyErr_NoMemory();
+        }
+        else {
+            PyErr_SetString(PyExc_ValueError,
+                "format specification exceeds internal limits of _decimal");
+        }
         goto finish;
     }
     result = PyUnicode_DecodeUTF8(decstring, strlen(decstring), NULL);