]> granicus.if.org Git - python/commitdiff
Accept Unicode legacy strings in the Decimal constructor.
authorStefan Krah <skrah@bytereef.org>
Sat, 10 Nov 2012 22:09:04 +0000 (23:09 +0100)
committerStefan Krah <skrah@bytereef.org>
Sat, 10 Nov 2012 22:09:04 +0000 (23:09 +0100)
Lib/test/test_decimal.py
Modules/_decimal/_decimal.c

index dd4c73cdf14bbb4da44f7578be7b99abd97fea96..5195930834d1f38d1f48d905dce0aa44ee39b1db 100644 (file)
@@ -35,7 +35,7 @@ import locale
 from test.support import (run_unittest, run_doctest, is_resource_enabled,
                           requires_IEEE_754)
 from test.support import (check_warnings, import_fresh_module, TestFailed,
-                          run_with_locale)
+                          run_with_locale, cpython_only)
 import random
 import time
 import warnings
@@ -574,6 +574,15 @@ class ExplicitConstructionTest(unittest.TestCase):
             # embedded NUL
             self.assertRaises(InvalidOperation, Decimal, "12\u00003")
 
+    @cpython_only
+    def test_from_legacy_strings(self):
+        import _testcapi
+        Decimal = self.decimal.Decimal
+        context = self.decimal.Context()
+
+        s = _testcapi.unicode_legacy_string('9.999999')
+        self.assertEqual(str(Decimal(s)), '9.999999')
+        self.assertEqual(str(context.create_decimal(s)), '9.999999')
 
     def test_explicit_from_tuples(self):
         Decimal = self.decimal.Decimal
index e951ded5fff011f78277555eea2c536506e2fc9d..0610a8bcec0c4aa8b02fdc68f6eae4e00ecb6fc5 100644 (file)
@@ -1892,7 +1892,9 @@ numeric_as_ascii(const PyObject *u, int strip_ws)
     Py_ssize_t j, len;
     int d;
 
-    assert(PyUnicode_IS_READY(u));
+    if (PyUnicode_READY(u) == -1) {
+        return NULL;
+    }
 
     kind = PyUnicode_KIND(u);
     data = PyUnicode_DATA(u);