From: Isaac Jurado Date: Sun, 4 Oct 2015 18:49:23 +0000 (+0200) Subject: numbers: Use cdecimal by default when available X-Git-Tag: dev-2a51c9b95d06~2^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b6169be329e121111dc9c3242c3538ee3b0a3aaa;p=thirdparty%2Fbabel.git numbers: Use cdecimal by default when available The drop-in replacement cdecimal is a CPython extension that implements the same decimal interface with a much better performance. Whenever it is installed, we favour its use. --- diff --git a/babel/_compat.py b/babel/_compat.py index 0f7640de..78cd35a3 100644 --- a/babel/_compat.py +++ b/babel/_compat.py @@ -54,3 +54,22 @@ else: number_types = integer_types + (float,) + + +# +# Use cdecimal when available +# +from decimal import (Decimal as _dec, + InvalidOperation as _invop, + ROUND_HALF_EVEN as _RHE) +try: + from cdecimal import (Decimal as _cdec, + InvalidOperation as _cinvop, + ROUND_HALF_EVEN as _CRHE) + Decimal = _cdec + InvalidOperation = (_invop, _cinvop) + ROUND_HALF_EVEN = _CRHE +except ImportError: + Decimal = _dec + InvalidOperation = _invop + ROUND_HALF_EVEN = _RHE diff --git a/babel/numbers.py b/babel/numbers.py index f92c714c..a15f399b 100644 --- a/babel/numbers.py +++ b/babel/numbers.py @@ -20,10 +20,9 @@ # - http://www.unicode.org/reports/tr35/ (Appendix G.6) import re from datetime import date as date_, datetime as datetime_ -from decimal import Decimal, InvalidOperation, ROUND_HALF_EVEN from babel.core import default_locale, Locale, get_global -from babel._compat import range_type +from babel._compat import range_type, Decimal, InvalidOperation, ROUND_HALF_EVEN LC_NUMERIC = default_locale('LC_NUMERIC')