diff --git a/cf_units/__init__.py b/cf_units/__init__.py index 00d51f97..b5dddaa7 100644 --- a/cf_units/__init__.py +++ b/cf_units/__init__.py @@ -14,7 +14,9 @@ """ import copy +import locale import math +import threading from contextlib import contextmanager from warnings import warn @@ -175,11 +177,25 @@ def suppress_errors(): _ud.set_error_message_handler(_default_handler) +LOCALE_LOCK = threading.Lock() + + +@contextmanager +def c_locale(): + with LOCALE_LOCK: + lc_numeric = locale.getlocale(locale.LC_NUMERIC) + locale.setlocale(locale.LC_NUMERIC, "C") + try: + yield + finally: + locale.setlocale(locale.LC_NUMERIC, lc_numeric) + + # # load the UDUNITS-2 xml-formatted unit-database #: # Ignore standard noisy UDUNITS-2 start-up. -with suppress_errors(): +with suppress_errors(), c_locale(): # Load the unit-database from the default location (modified via # the UDUNITS2_XML_PATH environment variable) and if that fails look # relative to sys.prefix to support environments such as conda.