]> granicus.if.org Git - python/commitdiff
Issue #2478: Decimal(sqrt(0)) failed when the decimal context
authorMark Dickinson <dickinsm@gmail.com>
Tue, 25 Mar 2008 14:33:23 +0000 (14:33 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Tue, 25 Mar 2008 14:33:23 +0000 (14:33 +0000)
was not explicitly supplied.

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

index 5d25012bd0722966cb29d908fe3e07d0136e1753..b775bee3e3ccecbfbd97a5cece35a04ece6a5b1a 100644 (file)
@@ -2453,6 +2453,9 @@ class Decimal(object):
 
     def sqrt(self, context=None):
         """Return the square root of self."""
+        if context is None:
+            context = getcontext()
+
         if self._is_special:
             ans = self._check_nans(context=context)
             if ans:
@@ -2466,9 +2469,6 @@ class Decimal(object):
             ans = _dec_from_triple(self._sign, '0', self._exp // 2)
             return ans._fix(context)
 
-        if context is None:
-            context = getcontext()
-
         if self._sign == 1:
             return context._raise_error(InvalidOperation, 'sqrt(-x), x > 0')
 
index 5e533185f8d5c1b68f36aa709843e0aeb8b76d08..46d2dc71216bf76338677f9aefbbc6013fc126d3 100644 (file)
@@ -1315,6 +1315,12 @@ class DecimalUsabilityTest(unittest.TestCase):
         d = d1.max(d2)
         self.assertTrue(type(d) is Decimal)
 
+    def test_implicit_context(self):
+        # Check results when context given implicitly.  (Issue 2478)
+        c = getcontext()
+        self.assertEqual(str(Decimal(0).sqrt()),
+                         str(c.sqrt(Decimal(0))))
+
 
 class DecimalPythonAPItests(unittest.TestCase):
 
index 4afd6cad10bb85dfcc1c2b7be3300ff2f66f9034..65815f7ab2423255f0024a22df031903462fb7c5 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -72,6 +72,8 @@ Extensions Modules
 Library
 -------
 
+- Issue #2478: fix failure of decimal.Decimal(0).sqrt()
+
 - Issue #2432: give DictReader the dialect and line_num attributes
   advertised in the docs.