From b6169be329e121111dc9c3242c3538ee3b0a3aaa Mon Sep 17 00:00:00 2001 From: Isaac Jurado Date: Sun, 4 Oct 2015 20:49:23 +0200 Subject: [PATCH] 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. --- babel/_compat.py | 19 +++++++++++++++++++ babel/numbers.py | 3 +-- 2 files changed, 20 insertions(+), 2 deletions(-) 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') -- 2.47.2