Skip to content

CLN: catch specific Exceptions in _config #28310

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 4 commits into from
Sep 7, 2019
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
5 changes: 4 additions & 1 deletion pandas/_config/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ def detect_console_encoding():
if not encoding or "ascii" in encoding.lower():
try:
encoding = locale.getpreferredencoding()
except Exception:
except locale.Error:
# can be raised by locale.setlocale(), which is
# called by getpreferredencoding
# (on some systems, see stdlib locale docs)
pass

# when all else fails. this will usually be "ascii"
Expand Down
12 changes: 4 additions & 8 deletions pandas/_config/localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,7 @@ def _valid_locales(locales, normalize):


def _default_locale_getter():
try:
raw_locales = subprocess.check_output(["locale -a"], shell=True)
except subprocess.CalledProcessError as e:
raise type(e)(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apparently in the status quo this ends up raising TypeError because we are calling the CalledProcessError constructor incorrectly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we even need this at all then? Seems like it wasn't doing what was expected before so maybe can remove altogether?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering this myself. Will change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed per request (and green)

"{exception}, the 'locale -a' command cannot be found "
"on your system".format(exception=e)
)
raw_locales = subprocess.check_output(["locale -a"], shell=True)
return raw_locales


Expand Down Expand Up @@ -139,7 +133,9 @@ def get_locales(prefix=None, normalize=True, locale_getter=_default_locale_gette
"""
try:
raw_locales = locale_getter()
except Exception:
except subprocess.CalledProcessError:
# Raised on (some? all?) Windows platforms because Note: "locale -a"
# is not defined
return None

try:
Expand Down
8 changes: 5 additions & 3 deletions pandas/tests/io/formats/test_console.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import locale

import pytest

from pandas._config import detect_console_encoding
Expand Down Expand Up @@ -50,11 +52,11 @@ def test_detect_console_encoding_fallback_to_locale(monkeypatch, encoding):
"std,locale",
[
["ascii", "ascii"],
["ascii", Exception],
["ascii", locale.Error],
[AttributeError, "ascii"],
[AttributeError, Exception],
[AttributeError, locale.Error],
[IOError, "ascii"],
[IOError, Exception],
[IOError, locale.Error],
],
)
def test_detect_console_encoding_fallback_to_default(monkeypatch, std, locale):
Expand Down