Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid issues with LC_NUMERIC locale #436

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading