]> granicus.if.org Git - python/commitdiff
Issue #2482: Make sure that the coefficient of a Decimal
authorMark Dickinson <dickinsm@gmail.com>
Tue, 25 Mar 2008 18:47:59 +0000 (18:47 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Tue, 25 Mar 2008 18:47:59 +0000 (18:47 +0000)
instance is always stored as a str instance, even
when that Decimal has been created from a unicode string.

Lib/decimal.py
Lib/test/test_decimal.py
Misc/NEWS

index b775bee3e3ccecbfbd97a5cece35a04ece6a5b1a..dc441709000f70d9d64ae9f033e756558099b806 100644 (file)
@@ -557,17 +557,17 @@ class Decimal(object):
                 fracpart = m.group('frac')
                 exp = int(m.group('exp') or '0')
                 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)
                 else:
-                    self._int = intpart.lstrip('0') or '0'
+                    self._int = str(intpart.lstrip('0') or '0')
                     self._exp = exp
                 self._is_special = False
             else:
                 diag = m.group('diag')
                 if diag is not None:
                     # NaN
-                    self._int = diag.lstrip('0')
+                    self._int = str(diag.lstrip('0'))
                     if m.group('signal'):
                         self._exp = 'N'
                     else:
index 46d2dc71216bf76338677f9aefbbc6013fc126d3..ce6acb677d76ad7e17b0700843e4d3c538a3a80e 100644 (file)
@@ -434,6 +434,12 @@ class DecimalExplicitConstructionTest(unittest.TestCase):
         self.assertEqual(str(Decimal('1.3E4 \n')), '1.3E+4')
         self.assertEqual(str(Decimal('  -7.89')), '-7.89')
 
+        #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):
 
         #zero
@@ -1149,6 +1155,16 @@ class DecimalUsabilityTest(unittest.TestCase):
         self.assertEqual(str(d), '15.32')               # str
         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):
         #Test float, int and long methods.
 
index 65815f7ab2423255f0024a22df031903462fb7c5..9c3193d6261579aba12a52a2bd5abd0a1c9eb235 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -72,6 +72,10 @@ Extensions Modules
 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.
+
 - Issue #2478: fix failure of decimal.Decimal(0).sqrt()
 
 - Issue #2432: give DictReader the dialect and line_num attributes