]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Register decimals as numbers.Number
authorRaymond Hettinger <python@rcn.com>
Tue, 3 Feb 2009 03:42:07 +0000 (03:42 +0000)
committerRaymond Hettinger <python@rcn.com>
Tue, 3 Feb 2009 03:42:07 +0000 (03:42 +0000)
Lib/decimal.py
Lib/test/test_decimal.py
Misc/NEWS

index 661d6ed4af8534dfaae177cdbd8205fa906d3012..3ad80b352950b7ce2b0954d2125c002614b382be 100644 (file)
@@ -135,6 +135,7 @@ __all__ = [
 ]
 
 import copy as _copy
+import numbers as _numbers
 
 try:
     from collections import namedtuple as _namedtuple
@@ -3567,6 +3568,12 @@ def _dec_from_triple(sign, coefficient, exponent, special=False):
 
     return self
 
+# Register Decimal as a kind of Number (an abstract base class).
+# However, do not register it as Real (because Decimals are not
+# interoperable with floats).
+_numbers.Number.register(Decimal)
+
+
 ##### Context class #######################################################
 
 
index b9955579dc10233f488a5f3396e94250a4991b16..114cd5b79008196ba6d44f221a41a14f3e7b4cad 100644 (file)
@@ -30,6 +30,7 @@ import os, sys
 import pickle, copy
 import unittest
 from decimal import *
+import numbers
 from test.test_support import (TestSkipped, run_unittest, run_doctest,
                                is_resource_enabled)
 import random
@@ -1334,6 +1335,12 @@ class DecimalUsabilityTest(unittest.TestCase):
 
 class DecimalPythonAPItests(unittest.TestCase):
 
+    def test_abc(self):
+        self.assert_(issubclass(Decimal, numbers.Number))
+        self.assert_(not issubclass(Decimal, numbers.Real))
+        self.assert_(isinstance(Decimal(0), numbers.Number))
+        self.assert_(not isinstance(Decimal(0), numbers.Real))
+
     def test_pickle(self):
         d = Decimal('-3.141590000')
         p = pickle.dumps(d)
index ebff3a152cd86b458905ad2bd7be98f4218b4ae6..2be82dcd3534f943aa082aa9a321ef7cc27463be 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -213,6 +213,8 @@ Library
 - Issue #1885: distutils. When running sdist with --formats=tar,gztar
   the tar file was overriden by the gztar one.
 
+- Registered Decimal as a numbers.Number.
+
 - Issue #1672332: fix unpickling of subnormal floats, which was
   producing a ValueError on some platforms.