diff options
author | Isaac Jurado <diptongo@gmail.com> | 2015-10-04 20:49:23 +0200 |
---|---|---|
committer | Isaac Jurado <diptongo@gmail.com> | 2015-10-14 19:54:12 +0200 |
commit | b6169be329e121111dc9c3242c3538ee3b0a3aaa (patch) | |
tree | 0a9c691b4a5c9c6c3ced7af5154b69d9f77bd9ee | |
parent | d177d5eb746693e0e7bb861ece87a4824008c649 (diff) | |
download | babel-b6169be329e121111dc9c3242c3538ee3b0a3aaa.tar.gz |
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.
-rw-r--r-- | babel/_compat.py | 19 | ||||
-rw-r--r-- | babel/numbers.py | 3 |
2 files changed, 20 insertions, 2 deletions
diff --git a/babel/_compat.py b/babel/_compat.py index 0f7640d..78cd35a 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 f92c714..a15f399 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') |