diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2008-03-25 14:35:25 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2008-03-25 14:35:25 (GMT) |
commit | 26c25d9f64cce575820f1d1f13c757020829888d (patch) | |
tree | 1b864f99ac95483c46104f28f9db714cedd6ba56 | |
parent | a79e05097be27579d0ab0e38fda404b0edb670c0 (diff) | |
download | cpython-26c25d9f64cce575820f1d1f13c757020829888d.zip cpython-26c25d9f64cce575820f1d1f13c757020829888d.tar.gz cpython-26c25d9f64cce575820f1d1f13c757020829888d.tar.bz2 |
Decimal.sqrt(0) failed when the context was not
explicitly supplied.
-rw-r--r-- | Lib/decimal.py | 6 | ||||
-rw-r--r-- | Lib/test/test_decimal.py | 6 | ||||
-rw-r--r-- | Misc/NEWS | 2 |
3 files changed, 11 insertions, 3 deletions
diff --git a/Lib/decimal.py b/Lib/decimal.py index ebeb6d7..8b3e252 100644 --- a/Lib/decimal.py +++ b/Lib/decimal.py @@ -2316,6 +2316,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: @@ -2329,9 +2332,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') diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index 34fc34a..efaa174 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -1192,6 +1192,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): @@ -27,6 +27,8 @@ Core and builtins Library ------- +- Issue #2478: fix failure of decimal.Decimal(0).sqrt() + - Issue #2432: give DictReader the dialect and line_num attributes advertised in the docs. |