From 8a0d72770d38ac44343d98dc2674ee87a847b701 Mon Sep 17 00:00:00 2001 From: Graham Inggs Date: Mon, 6 Aug 2018 12:49:58 +0200 Subject: [PATCH 1/2] Fix Python2 test failures in certain locales Check that we can also get the locale, after setting it, without raising an Exception. Closes: #22129 --- pandas/tests/util/test_util.py | 26 ++++++++++++++++++++++++++ pandas/util/testing.py | 19 ++++++++----------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/pandas/tests/util/test_util.py b/pandas/tests/util/test_util.py index dabafb1f49ba8..1549e80672b5c 100644 --- a/pandas/tests/util/test_util.py +++ b/pandas/tests/util/test_util.py @@ -417,6 +417,32 @@ def test_numpy_errstate_is_default(): assert np.geterr() == expected +@td.skip_if_windows +def test_can_set_locale_valid_set(): + # Setting the default locale should return True + assert tm.can_set_locale('') is True + + +@td.skip_if_windows +def test_can_set_locale_invalid_set(): + # Setting an invalid locale should return False + assert tm.can_set_locale('non-existent_locale') is False + + +@td.skip_if_windows +def test_can_set_locale_invalid_get(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 + + @td.skip_if_windows class TestLocaleUtils(object): diff --git a/pandas/util/testing.py b/pandas/util/testing.py index afc928ddfbb84..39ab498d080bf 100644 --- a/pandas/util/testing.py +++ b/pandas/util/testing.py @@ -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 ---------- @@ -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 From 6cceebc7bd03b229bddfbbb6f0064b596991d6d7 Mon Sep 17 00:00:00 2001 From: Graham Inggs Date: Sun, 12 Aug 2018 13:05:10 +0200 Subject: [PATCH 2/2] Move tests into TestLocaleUtils class --- pandas/tests/util/test_util.py | 46 +++++++++++++++------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/pandas/tests/util/test_util.py b/pandas/tests/util/test_util.py index 1549e80672b5c..c049dfc874940 100644 --- a/pandas/tests/util/test_util.py +++ b/pandas/tests/util/test_util.py @@ -417,32 +417,6 @@ def test_numpy_errstate_is_default(): assert np.geterr() == expected -@td.skip_if_windows -def test_can_set_locale_valid_set(): - # Setting the default locale should return True - assert tm.can_set_locale('') is True - - -@td.skip_if_windows -def test_can_set_locale_invalid_set(): - # Setting an invalid locale should return False - assert tm.can_set_locale('non-existent_locale') is False - - -@td.skip_if_windows -def test_can_set_locale_invalid_get(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 - - @td.skip_if_windows class TestLocaleUtils(object): @@ -459,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