Index: Lib/decimal.py =================================================================== --- Lib/decimal.py (revision 74247) +++ Lib/decimal.py (working copy) @@ -554,20 +554,16 @@ 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).lstrip('0') or '0')) + 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 @@ # 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[-+])? # an optional sign, followed by either... ( - (?=[0-9]|\.[0-9]) # ...a number (with at least one digit) - (?P[0-9]*) # having a (possibly empty) integer part - (\.(?P[0-9]*))? # followed by an optional fractional part - (E(?P[-+]?[0-9]+))? # followed by an optional exponent, or... + (?=\d|\.\d) # ...a number (with at least one digit) + (?P\d*) # having a (possibly empty) integer part + (\.(?P\d*))? # followed by an optional fractional part + (E(?P[-+]?\d+))? # followed by an optional exponent, or... | Inf(inity)? # ...an infinity, or... | (?Ps)? # ...an (optionally signaling) NaN # NaN - (?P[0-9]*) # with (possibly empty) diagnostic info. + (?P\d*) # with (possibly empty) diagnostic info. ) # \s* \Z Index: Lib/test/test_decimal.py =================================================================== --- Lib/test/test_decimal.py (revision 74247) +++ Lib/test/test_decimal.py (working copy) @@ -425,9 +425,6 @@ 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,7 +531,16 @@ 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.'''