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

index e9280c59b28a2c1722212ddc14ddb401ad352950..7b1308d1f4008de8a1c70078d0a5012e50d4828a 100644 (file)
@@ -135,6 +135,7 @@ __all__ = [
 ]
 
 import copy as _copy
+import numbers as _numbers
 
 try:
     from collections import namedtuple as _namedtuple
@@ -3654,6 +3655,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 fde37fe0b4a399ceb460b08ede46c5bd756a4d9a..6b51d13b099fe61ac14282e68d5541077f4c11d9 100644 (file)
@@ -30,6 +30,7 @@ import os, sys
 import pickle, copy
 import unittest
 from decimal import *
+import numbers
 from test.support import (TestSkipped, run_unittest, run_doctest,
                                is_resource_enabled)
 import random
@@ -1389,6 +1390,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 b516a637821ec82a410c437a48eed457c3886cdd..ae32ca16636f1a2c598da88b1f9352f30c1ed1ea 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -152,7 +152,8 @@ Library
   rather than delaying the close by waiting for garbage collection to do it.
 
 - Issue #4998: Decimal no longer subclasses from or is registered to
-  numbers.Real.
+  numbers.Real.  Instead, it is registered to numbers.Number so that
+  isinstance(d, Number) will work.
 
 - Issue #4867: Fixed a crash in ctypes when passing a string to a
   function without defining argtypes.