]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
numbers: Use cdecimal by default when available
authorIsaac Jurado <diptongo@gmail.com>
Sun, 4 Oct 2015 18:49:23 +0000 (20:49 +0200)
committerIsaac Jurado <diptongo@gmail.com>
Wed, 14 Oct 2015 17:54:12 +0000 (19:54 +0200)
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
babel/numbers.py

index 0f7640de1d5bb750652efddf735fae10d23afcbb..78cd35a379a4111ae66131b950b7e3b63fa1f6b8 100644 (file)
@@ -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
index f92c714caed8f5777be292dfe70bb17f310b78e8..a15f399bba56c2948e31c030f282ce251837ccea 100644 (file)
 #  - 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')