diff --git a/pandas/_config/localization.py b/pandas/_config/localization.py index eaae30ee9098b..4e9a0142af3a4 100644 --- a/pandas/_config/localization.py +++ b/pandas/_config/localization.py @@ -7,12 +7,10 @@ from contextlib import contextmanager import locale +import platform import re import subprocess -from typing import ( - Callable, - Generator, -) +from typing import Generator from pandas._config.config import options @@ -107,15 +105,10 @@ def _valid_locales(locales: list[str] | str, normalize: bool) -> list[str]: ] -def _default_locale_getter() -> bytes: - return subprocess.check_output(["locale -a"], shell=True) - - def get_locales( prefix: str | None = None, normalize: bool = True, - locale_getter: Callable[[], bytes] = _default_locale_getter, -) -> list[str] | None: +) -> list[str]: """ Get all the locales that are available on the system. @@ -129,9 +122,6 @@ def get_locales( Call ``locale.normalize`` on the resulting list of available locales. If ``True``, only locales that can be set without throwing an ``Exception`` are returned. - locale_getter : callable - The function to use to retrieve the current locales. This should return - a string with each locale separated by a newline character. Returns ------- @@ -141,15 +131,15 @@ def get_locales( locale.setlocale(locale.LC_ALL, locale_string) - On error will return None (no locale available, e.g. Windows) + On error will return an empty list (no locale available, e.g. Windows) """ - try: - raw_locales = locale_getter() - except subprocess.CalledProcessError: - # Raised on (some? all?) Windows platforms because Note: "locale -a" - # is not defined - return None + if platform.system() in ("Linux", "Darwin"): + raw_locales = subprocess.check_output(["locale", "-a"]) + else: + # Other platforms e.g. windows platforms don't define "locale -a" + # Note: is_platform_windows causes circular import here + return [] try: # raw_locales is "\n" separated list of locales diff --git a/pandas/tests/config/test_localization.py b/pandas/tests/config/test_localization.py index f972a9ee3b497..6474177261b91 100644 --- a/pandas/tests/config/test_localization.py +++ b/pandas/tests/config/test_localization.py @@ -12,7 +12,7 @@ import pandas as pd -_all_locales = get_locales() or [] +_all_locales = get_locales() _current_locale = locale.setlocale(locale.LC_ALL) # getlocale() is wrong, see GH#46595 # Don't run any of these tests if we have no locales. diff --git a/pandas/tests/indexes/datetimes/test_misc.py b/pandas/tests/indexes/datetimes/test_misc.py index d4a87e6a8b65c..c69c35ee46307 100644 --- a/pandas/tests/indexes/datetimes/test_misc.py +++ b/pandas/tests/indexes/datetimes/test_misc.py @@ -193,7 +193,7 @@ def test_datetimeindex_accessors6(self): # GH 12806 # error: Unsupported operand types for + ("List[None]" and "List[str]") @pytest.mark.parametrize( - "time_locale", [None] + (tm.get_locales() or []) # type: ignore[operator] + "time_locale", [None] + tm.get_locales() # type: ignore[operator] ) def test_datetime_name_accessors(self, time_locale): # Test Monday -> Sunday and January -> December, in that sequence diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index c195b96a1500d..61663f774ced0 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -153,7 +153,7 @@ def test_is_end(self, end, tz): ) # error: Unsupported operand types for + ("List[None]" and "List[str]") @pytest.mark.parametrize( - "time_locale", [None] + (tm.get_locales() or []) # type: ignore[operator] + "time_locale", [None] + tm.get_locales() # type: ignore[operator] ) def test_names(self, data, time_locale): # GH 17354 diff --git a/pandas/tests/series/accessors/test_dt_accessor.py b/pandas/tests/series/accessors/test_dt_accessor.py index 11ad7c1dd457e..47e59be907929 100644 --- a/pandas/tests/series/accessors/test_dt_accessor.py +++ b/pandas/tests/series/accessors/test_dt_accessor.py @@ -432,7 +432,7 @@ def test_dt_accessor_no_new_attributes(self): # error: Unsupported operand types for + ("List[None]" and "List[str]") @pytest.mark.parametrize( - "time_locale", [None] + (tm.get_locales() or []) # type: ignore[operator] + "time_locale", [None] + tm.get_locales() # type: ignore[operator] ) def test_dt_accessor_datetime_name_accessors(self, time_locale): # Test Monday -> Sunday and January -> December, in that sequence