1
0

test_localization.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import codecs
  2. import locale
  3. import os
  4. import pytest
  5. from pandas._config.localization import can_set_locale, get_locales, set_locale
  6. from pandas.compat import is_platform_windows
  7. import pandas as pd
  8. _all_locales = get_locales() or []
  9. _current_locale = locale.getlocale()
  10. # Don't run any of these tests if we are on Windows or have no locales.
  11. pytestmark = pytest.mark.skipif(
  12. is_platform_windows() or not _all_locales, reason="Need non-Windows and locales"
  13. )
  14. _skip_if_only_one_locale = pytest.mark.skipif(
  15. len(_all_locales) <= 1, reason="Need multiple locales for meaningful test"
  16. )
  17. def test_can_set_locale_valid_set():
  18. # Can set the default locale.
  19. assert can_set_locale("")
  20. def test_can_set_locale_invalid_set():
  21. # Cannot set an invalid locale.
  22. assert not can_set_locale("non-existent_locale")
  23. def test_can_set_locale_invalid_get(monkeypatch):
  24. # see GH#22129
  25. # In some cases, an invalid locale can be set,
  26. # but a subsequent getlocale() raises a ValueError.
  27. def mock_get_locale():
  28. raise ValueError()
  29. with monkeypatch.context() as m:
  30. m.setattr(locale, "getlocale", mock_get_locale)
  31. assert not can_set_locale("")
  32. def test_get_locales_at_least_one():
  33. # see GH#9744
  34. assert len(_all_locales) > 0
  35. @_skip_if_only_one_locale
  36. def test_get_locales_prefix():
  37. first_locale = _all_locales[0]
  38. assert len(get_locales(prefix=first_locale[:2])) > 0
  39. @_skip_if_only_one_locale
  40. @pytest.mark.parametrize(
  41. "lang,enc",
  42. [
  43. ("it_CH", "UTF-8"),
  44. ("en_US", "ascii"),
  45. ("zh_CN", "GB2312"),
  46. ("it_IT", "ISO-8859-1"),
  47. ],
  48. )
  49. def test_set_locale(lang, enc):
  50. if all(x is None for x in _current_locale):
  51. # Not sure why, but on some Travis runs with pytest,
  52. # getlocale() returned (None, None).
  53. pytest.skip("Current locale is not set.")
  54. enc = codecs.lookup(enc).name
  55. new_locale = lang, enc
  56. if not can_set_locale(new_locale):
  57. msg = "unsupported locale setting"
  58. with pytest.raises(locale.Error, match=msg):
  59. with set_locale(new_locale):
  60. pass
  61. else:
  62. with set_locale(new_locale) as normalized_locale:
  63. new_lang, new_enc = normalized_locale.split(".")
  64. new_enc = codecs.lookup(enc).name
  65. normalized_locale = new_lang, new_enc
  66. assert normalized_locale == new_locale
  67. # Once we exit the "with" statement, locale should be back to what it was.
  68. current_locale = locale.getlocale()
  69. assert current_locale == _current_locale
  70. def test_encoding_detected():
  71. system_locale = os.environ.get("LC_ALL")
  72. system_encoding = system_locale.split(".")[-1] if system_locale else "utf-8"
  73. assert (
  74. codecs.lookup(pd.options.display.encoding).name
  75. == codecs.lookup(system_encoding).name
  76. )