]> granicus.if.org Git - python/commitdiff
Register decimals as numbers.Number
authorRaymond Hettinger <python@rcn.com>
Tue, 3 Feb 2009 03:37:03 +0000 (03:37 +0000)
committerRaymond Hettinger <python@rcn.com>
Tue, 3 Feb 2009 03:37:03 +0000 (03:37 +0000)
Lib/decimal.py
Lib/test/test_decimal.py

index 87bd142048aea6a988d1d4f281541c046778800d..dfc7043dde0f0de80e0c2882424685446038854d 100644 (file)
@@ -136,6 +136,7 @@ __all__ = [
 
 import copy as _copy
 import math as _math
+import numbers as _numbers
 
 try:
     from collections import namedtuple as _namedtuple
@@ -3608,6 +3609,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 da870083d641cec17d83384328778dc035a85f94..65aa03322583c3c31c0876b3638db92826190cc0 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)