Skip to content

TST/CLN: Avoid subprocess shell=True #49033

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 6 commits into from
Oct 13, 2022
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
30 changes: 10 additions & 20 deletions pandas/_config/localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.

Expand All @@ -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
-------
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/config/test_localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/datetimes/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/scalar/timestamp/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/accessors/test_dt_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down