Skip to content

Commit

Permalink
Avoid issues with LC_NUMERIC locale (#436)
Browse files Browse the repository at this point in the history
* Avoid issues with LC_NUMERIC locale

* Use C locale instead of en_US as that may not be installed on all systems

---------

Co-authored-by: Patrick Peglar <[email protected]>
  • Loading branch information
bouweandela and pp-mo authored Oct 30, 2024
1 parent 54b2819 commit 792ba1e
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion cf_units/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"""

import copy
import locale
import math
import threading
from contextlib import contextmanager
from warnings import warn

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 792ba1e

Please sign in to comment.