Skip to content

Fix Python2 test failures in certain locales #22213

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

Merged
merged 2 commits into from
Aug 13, 2018
Merged
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions pandas/tests/util/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,26 @@ def teardown_class(cls):
del cls.locales
del cls.current_locale

def test_can_set_locale_valid_set(self):
# Setting the default locale should return True
assert tm.can_set_locale('') is True

def test_can_set_locale_invalid_set(self):
# Setting an invalid locale should return False
assert tm.can_set_locale('non-existent_locale') is False

def test_can_set_locale_invalid_get(self, monkeypatch):
# In some cases, an invalid locale can be set,
# but a subsequent getlocale() raises a ValueError
# See GH 22129

def mockgetlocale():
raise ValueError()

with monkeypatch.context() as m:
m.setattr(locale, 'getlocale', mockgetlocale)
assert tm.can_set_locale('') is False

def test_get_locales(self):
# all systems should have at least a single locale
assert len(tm.get_locales()) > 0
Expand Down
19 changes: 8 additions & 11 deletions pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,23 +504,19 @@ def set_locale(new_locale, lc_var=locale.LC_ALL):

try:
locale.setlocale(lc_var, new_locale)

try:
normalized_locale = locale.getlocale()
except ValueError:
yield new_locale
normalized_locale = locale.getlocale()
if com._all_not_none(*normalized_locale):
yield '.'.join(normalized_locale)
else:
if com._all_not_none(*normalized_locale):
yield '.'.join(normalized_locale)
else:
yield new_locale
yield new_locale
finally:
locale.setlocale(lc_var, current_locale)


def can_set_locale(lc, lc_var=locale.LC_ALL):
"""
Check to see if we can set a locale without raising an Exception.
Check to see if we can set a locale, and subsequently get the locale,
without raising an Exception.

Parameters
----------
Expand All @@ -538,7 +534,8 @@ def can_set_locale(lc, lc_var=locale.LC_ALL):
try:
with set_locale(lc, lc_var=lc_var):
pass
except locale.Error: # horrible name for a Exception subclass
except (ValueError,
locale.Error): # horrible name for a Exception subclass
return False
else:
return True
Expand Down