From 25ae68ce6c011d4284eb6df0c559df9d13746ea9 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 3 Feb 2009 03:50:39 +0000 Subject: [PATCH] Register decimals as numbers.Number --- Lib/decimal.py | 7 +++++++ Lib/test/test_decimal.py | 7 +++++++ Misc/NEWS | 3 ++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Lib/decimal.py b/Lib/decimal.py index e9280c59b28a..7b1308d1f400 100644 --- a/Lib/decimal.py +++ b/Lib/decimal.py @@ -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 ####################################################### diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index fde37fe0b4a3..6b51d13b099f 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -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) diff --git a/Misc/NEWS b/Misc/NEWS index b516a637821e..ae32ca16636f 100644 --- 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. -- 2.47.3