Skip to content

Commit 7161b90

Browse files
jbrockmendelWillAyd
authored andcommitted
CLN: catch specific Exceptions in _config (#28310)
1 parent 1cd7ae6 commit 7161b90

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

pandas/_config/display.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ def detect_console_encoding():
2828
if not encoding or "ascii" in encoding.lower():
2929
try:
3030
encoding = locale.getpreferredencoding()
31-
except Exception:
31+
except locale.Error:
32+
# can be raised by locale.setlocale(), which is
33+
# called by getpreferredencoding
34+
# (on some systems, see stdlib locale docs)
3235
pass
3336

3437
# when all else fails. this will usually be "ascii"

pandas/_config/localization.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,7 @@ def _valid_locales(locales, normalize):
9898

9999

100100
def _default_locale_getter():
101-
try:
102-
raw_locales = subprocess.check_output(["locale -a"], shell=True)
103-
except subprocess.CalledProcessError as e:
104-
raise type(e)(
105-
"{exception}, the 'locale -a' command cannot be found "
106-
"on your system".format(exception=e)
107-
)
101+
raw_locales = subprocess.check_output(["locale -a"], shell=True)
108102
return raw_locales
109103

110104

@@ -139,7 +133,9 @@ def get_locales(prefix=None, normalize=True, locale_getter=_default_locale_gette
139133
"""
140134
try:
141135
raw_locales = locale_getter()
142-
except Exception:
136+
except subprocess.CalledProcessError:
137+
# Raised on (some? all?) Windows platforms because Note: "locale -a"
138+
# is not defined
143139
return None
144140

145141
try:

pandas/tests/io/formats/test_console.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import locale
2+
13
import pytest
24

35
from pandas._config import detect_console_encoding
@@ -50,11 +52,11 @@ def test_detect_console_encoding_fallback_to_locale(monkeypatch, encoding):
5052
"std,locale",
5153
[
5254
["ascii", "ascii"],
53-
["ascii", Exception],
55+
["ascii", locale.Error],
5456
[AttributeError, "ascii"],
55-
[AttributeError, Exception],
57+
[AttributeError, locale.Error],
5658
[IOError, "ascii"],
57-
[IOError, Exception],
59+
[IOError, locale.Error],
5860
],
5961
)
6062
def test_detect_console_encoding_fallback_to_default(monkeypatch, std, locale):

0 commit comments

Comments
 (0)