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

Skip to content

Commit 6e46704

Browse files
author
Stefan Krah
committed
Accept Unicode legacy strings in the Decimal constructor.
1 parent 6b0bdab commit 6e46704

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

Lib/test/test_decimal.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from test.support import (run_unittest, run_doctest, is_resource_enabled,
3636
requires_IEEE_754)
3737
from test.support import (check_warnings, import_fresh_module, TestFailed,
38-
run_with_locale)
38+
run_with_locale, cpython_only)
3939
import random
4040
import time
4141
import warnings
@@ -574,6 +574,15 @@ def test_explicit_from_string(self):
574574
# embedded NUL
575575
self.assertRaises(InvalidOperation, Decimal, "12\u00003")
576576

577+
@cpython_only
578+
def test_from_legacy_strings(self):
579+
import _testcapi
580+
Decimal = self.decimal.Decimal
581+
context = self.decimal.Context()
582+
583+
s = _testcapi.unicode_legacy_string('9.999999')
584+
self.assertEqual(str(Decimal(s)), '9.999999')
585+
self.assertEqual(str(context.create_decimal(s)), '9.999999')
577586

578587
def test_explicit_from_tuples(self):
579588
Decimal = self.decimal.Decimal

Modules/_decimal/_decimal.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1892,7 +1892,9 @@ numeric_as_ascii(const PyObject *u, int strip_ws)
18921892
Py_ssize_t j, len;
18931893
int d;
18941894

1895-
assert(PyUnicode_IS_READY(u));
1895+
if (PyUnicode_READY(u) == -1) {
1896+
return NULL;
1897+
}
18961898

18971899
kind = PyUnicode_KIND(u);
18981900
data = PyUnicode_DATA(u);

0 commit comments

Comments
 (0)