Skip to content

Commit 7aa391e

Browse files
authored
TST/CLN: Avoid subprocess shell=True (#49033)
1 parent 51a869c commit 7aa391e

File tree

5 files changed

+14
-24
lines changed

5 files changed

+14
-24
lines changed

pandas/_config/localization.py

+10-20
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77

88
from contextlib import contextmanager
99
import locale
10+
import platform
1011
import re
1112
import subprocess
12-
from typing import (
13-
Callable,
14-
Generator,
15-
)
13+
from typing import Generator
1614

1715
from pandas._config.config import options
1816

@@ -107,15 +105,10 @@ def _valid_locales(locales: list[str] | str, normalize: bool) -> list[str]:
107105
]
108106

109107

110-
def _default_locale_getter() -> bytes:
111-
return subprocess.check_output(["locale -a"], shell=True)
112-
113-
114108
def get_locales(
115109
prefix: str | None = None,
116110
normalize: bool = True,
117-
locale_getter: Callable[[], bytes] = _default_locale_getter,
118-
) -> list[str] | None:
111+
) -> list[str]:
119112
"""
120113
Get all the locales that are available on the system.
121114
@@ -129,9 +122,6 @@ def get_locales(
129122
Call ``locale.normalize`` on the resulting list of available locales.
130123
If ``True``, only locales that can be set without throwing an
131124
``Exception`` are returned.
132-
locale_getter : callable
133-
The function to use to retrieve the current locales. This should return
134-
a string with each locale separated by a newline character.
135125
136126
Returns
137127
-------
@@ -141,15 +131,15 @@ def get_locales(
141131
142132
locale.setlocale(locale.LC_ALL, locale_string)
143133
144-
On error will return None (no locale available, e.g. Windows)
134+
On error will return an empty list (no locale available, e.g. Windows)
145135
146136
"""
147-
try:
148-
raw_locales = locale_getter()
149-
except subprocess.CalledProcessError:
150-
# Raised on (some? all?) Windows platforms because Note: "locale -a"
151-
# is not defined
152-
return None
137+
if platform.system() in ("Linux", "Darwin"):
138+
raw_locales = subprocess.check_output(["locale", "-a"])
139+
else:
140+
# Other platforms e.g. windows platforms don't define "locale -a"
141+
# Note: is_platform_windows causes circular import here
142+
return []
153143

154144
try:
155145
# raw_locales is "\n" separated list of locales

pandas/tests/config/test_localization.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import pandas as pd
1414

15-
_all_locales = get_locales() or []
15+
_all_locales = get_locales()
1616
_current_locale = locale.setlocale(locale.LC_ALL) # getlocale() is wrong, see GH#46595
1717

1818
# Don't run any of these tests if we have no locales.

pandas/tests/indexes/datetimes/test_misc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def test_datetimeindex_accessors6(self):
193193
# GH 12806
194194
# error: Unsupported operand types for + ("List[None]" and "List[str]")
195195
@pytest.mark.parametrize(
196-
"time_locale", [None] + (tm.get_locales() or []) # type: ignore[operator]
196+
"time_locale", [None] + tm.get_locales() # type: ignore[operator]
197197
)
198198
def test_datetime_name_accessors(self, time_locale):
199199
# Test Monday -> Sunday and January -> December, in that sequence

pandas/tests/scalar/timestamp/test_timestamp.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def test_is_end(self, end, tz):
153153
)
154154
# error: Unsupported operand types for + ("List[None]" and "List[str]")
155155
@pytest.mark.parametrize(
156-
"time_locale", [None] + (tm.get_locales() or []) # type: ignore[operator]
156+
"time_locale", [None] + tm.get_locales() # type: ignore[operator]
157157
)
158158
def test_names(self, data, time_locale):
159159
# GH 17354

pandas/tests/series/accessors/test_dt_accessor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ def test_dt_accessor_no_new_attributes(self):
432432

433433
# error: Unsupported operand types for + ("List[None]" and "List[str]")
434434
@pytest.mark.parametrize(
435-
"time_locale", [None] + (tm.get_locales() or []) # type: ignore[operator]
435+
"time_locale", [None] + tm.get_locales() # type: ignore[operator]
436436
)
437437
def test_dt_accessor_datetime_name_accessors(self, time_locale):
438438
# Test Monday -> Sunday and January -> December, in that sequence

0 commit comments

Comments
 (0)