diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2009-08-02 10:14:23 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2009-08-02 10:14:23 (GMT) |
commit | 345adc43a37cf4640a58cd37df8eaf12f7e55dac (patch) | |
tree | 73f2f2382c799ac40da6d1007da9f904c2b27b3b /Lib | |
parent | 642d96a6476199840502a1e98d4902f7f4d47e78 (diff) | |
download | cpython-345adc43a37cf4640a58cd37df8eaf12f7e55dac.zip cpython-345adc43a37cf4640a58cd37df8eaf12f7e55dac.tar.gz cpython-345adc43a37cf4640a58cd37df8eaf12f7e55dac.tar.bz2 |
Issue #6595: Allow Decimal constructor to accept non-European decimal digits, as recommended by the specification.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/decimal.py | 27 | ||||
-rw-r--r-- | Lib/test/test_decimal.py | 12 |
2 files changed, 19 insertions, 20 deletions
diff --git a/Lib/decimal.py b/Lib/decimal.py index aac90d7..042dd8b 100644 --- a/Lib/decimal.py +++ b/Lib/decimal.py @@ -554,20 +554,16 @@ class Decimal(object): intpart = m.group('int') if intpart is not None: # finite number - fracpart = m.group('frac') + fracpart = m.group('frac') or '' exp = int(m.group('exp') or '0') - if fracpart is not None: - self._int = (intpart+fracpart).lstrip('0') or '0' - self._exp = exp - len(fracpart) - else: - self._int = intpart.lstrip('0') or '0' - self._exp = exp + self._int = str(int(intpart+fracpart)) + self._exp = exp - len(fracpart) self._is_special = False else: diag = m.group('diag') if diag is not None: # NaN - self._int = diag.lstrip('0') + self._int = str(int(diag or '0')).lstrip('0') if m.group('signal'): self._exp = 'N' else: @@ -5482,26 +5478,23 @@ ExtendedContext = Context( # 2. For finite numbers (not infinities and NaNs) the body of the # number between the optional sign and the optional exponent must have # at least one decimal digit, possibly after the decimal point. The -# lookahead expression '(?=[0-9]|\.[0-9])' checks this. -# -# As the flag UNICODE is not enabled here, we're explicitly avoiding any -# other meaning for \d than the numbers [0-9]. +# lookahead expression '(?=\d|\.\d)' checks this. import re _parser = re.compile(r""" # A numeric string consists of: # \s* (?P<sign>[-+])? # an optional sign, followed by either... ( - (?=[0-9]|\.[0-9]) # ...a number (with at least one digit) - (?P<int>[0-9]*) # having a (possibly empty) integer part - (\.(?P<frac>[0-9]*))? # followed by an optional fractional part - (E(?P<exp>[-+]?[0-9]+))? # followed by an optional exponent, or... + (?=\d|\.\d) # ...a number (with at least one digit) + (?P<int>\d*) # having a (possibly empty) integer part + (\.(?P<frac>\d*))? # followed by an optional fractional part + (E(?P<exp>[-+]?\d+))? # followed by an optional exponent, or... | Inf(inity)? # ...an infinity, or... | (?P<signal>s)? # ...an (optionally signaling) NaN # NaN - (?P<diag>[0-9]*) # with (possibly empty) diagnostic info. + (?P<diag>\d*) # with (possibly empty) diagnostic info. ) # \s* \Z diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index 43f2a08..927fd1a 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -425,9 +425,6 @@ class DecimalExplicitConstructionTest(unittest.TestCase): self.assertEqual(str(Decimal('1.3E4 \n')), '1.3E+4') self.assertEqual(str(Decimal(' -7.89')), '-7.89') - #but alternate unicode digits should not - self.assertEqual(str(Decimal('\uff11')), 'NaN') - def test_explicit_from_tuples(self): #zero @@ -534,6 +531,15 @@ class DecimalExplicitConstructionTest(unittest.TestCase): d = nc.create_decimal(prevdec) self.assertEqual(str(d), '5.00E+8') + def test_unicode_digits(self): + test_values = { + '\uff11': '1', + '\u0660.\u0660\u0663\u0667\u0662e-\u0663' : '0.0000372', + '-nan\u0c68\u0c6a\u0c66\u0c66' : '-NaN2400', + } + for input, expected in test_values.items(): + self.assertEqual(str(Decimal(input)), expected) + class DecimalImplicitConstructionTest(unittest.TestCase): '''Unit tests for Implicit Construction cases of Decimal.''' |