Commit 0e335a92 authored by Mark Dickinson's avatar Mark Dickinson

Issue #2482: Make sure that the coefficient of a Decimal instance

is stored as a str instance rather than a unicode instance.
Backported from Python 2.6 (see r61904).
parent f2c98a83
...@@ -549,17 +549,17 @@ class Decimal(object): ...@@ -549,17 +549,17 @@ class Decimal(object):
fracpart = m.group('frac') fracpart = m.group('frac')
exp = int(m.group('exp') or '0') exp = int(m.group('exp') or '0')
if fracpart is not None: if fracpart is not None:
self._int = (intpart+fracpart).lstrip('0') or '0' self._int = str((intpart+fracpart).lstrip('0') or '0')
self._exp = exp - len(fracpart) self._exp = exp - len(fracpart)
else: else:
self._int = intpart.lstrip('0') or '0' self._int = str(intpart.lstrip('0') or '0')
self._exp = exp self._exp = exp
self._is_special = False self._is_special = False
else: else:
diag = m.group('diag') diag = m.group('diag')
if diag is not None: if diag is not None:
# NaN # NaN
self._int = diag.lstrip('0') self._int = str(diag.lstrip('0'))
if m.group('signal'): if m.group('signal'):
self._exp = 'N' self._exp = 'N'
else: else:
......
...@@ -429,6 +429,12 @@ class DecimalExplicitConstructionTest(unittest.TestCase): ...@@ -429,6 +429,12 @@ class DecimalExplicitConstructionTest(unittest.TestCase):
#just not a number #just not a number
self.assertEqual(str(Decimal('ugly')), 'NaN') self.assertEqual(str(Decimal('ugly')), 'NaN')
#unicode strings should be permitted
self.assertEqual(str(Decimal(u'0E-017')), '0E-17')
self.assertEqual(str(Decimal(u'45')), '45')
self.assertEqual(str(Decimal(u'-Inf')), '-Infinity')
self.assertEqual(str(Decimal(u'NaN123')), 'NaN123')
def test_explicit_from_tuples(self): def test_explicit_from_tuples(self):
#zero #zero
...@@ -1032,6 +1038,16 @@ class DecimalUsabilityTest(unittest.TestCase): ...@@ -1032,6 +1038,16 @@ class DecimalUsabilityTest(unittest.TestCase):
self.assertEqual(str(d), '15.32') # str self.assertEqual(str(d), '15.32') # str
self.assertEqual(repr(d), 'Decimal("15.32")') # repr self.assertEqual(repr(d), 'Decimal("15.32")') # repr
# result type of string methods should be str, not unicode
unicode_inputs = [u'123.4', u'0.5E2', u'Infinity', u'sNaN',
u'-0.0E100', u'-NaN001', u'-Inf']
for u in unicode_inputs:
d = Decimal(u)
self.assertEqual(type(str(d)), str)
self.assertEqual(type(repr(d)), str)
self.assertEqual(type(d.to_eng_string()), str)
def test_tonum_methods(self): def test_tonum_methods(self):
#Test float, int and long methods. #Test float, int and long methods.
......
...@@ -27,6 +27,11 @@ Core and builtins ...@@ -27,6 +27,11 @@ Core and builtins
Library Library
------- -------
- Issue #2482: Make sure that the coefficient of a Decimal is always
stored as a str instance, not as a unicode instance. This ensures
that str(Decimal) is always an instance of str. This fixes a
regression from Python 2.5.1 to Python 2.5.2.
- Issue #2478: fix failure of decimal.Decimal(0).sqrt() - Issue #2478: fix failure of decimal.Decimal(0).sqrt()
- Issue #2432: give DictReader the dialect and line_num attributes - Issue #2432: give DictReader the dialect and line_num attributes
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment