From: Raymond Hettinger Date: Tue, 3 Feb 2009 03:42:07 +0000 (+0000) Subject: Register decimals as numbers.Number X-Git-Tag: v2.6.2c1~212 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=45fd4763fffeaeb9d3fbb3dee2f861fad85b8d2b;p=thirdparty%2FPython%2Fcpython.git Register decimals as numbers.Number --- diff --git a/Lib/decimal.py b/Lib/decimal.py index 661d6ed4af85..3ad80b352950 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 @@ -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 ####################################################### diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index b9955579dc10..114cd5b79008 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.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) diff --git a/Misc/NEWS b/Misc/NEWS index ebff3a152cd8..2be82dcd3534 100644 --- 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.