Skip to content

Commit 26b903b

Browse files
datapythonistahweecat
authored andcommitted
CLN: Clean up of locale testing (pandas-dev#29883)
1 parent 65c9cb8 commit 26b903b

File tree

5 files changed

+63
-40
lines changed

5 files changed

+63
-40
lines changed

ci/azure/posix.yml

+21-7
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,24 @@ jobs:
1919
ENV_FILE: ci/deps/azure-36-minimum_versions.yaml
2020
CONDA_PY: "36"
2121
PATTERN: "not slow and not network"
22+
2223
py36_locale_slow_old_np:
2324
ENV_FILE: ci/deps/azure-36-locale_slow.yaml
2425
CONDA_PY: "36"
2526
PATTERN: "slow"
26-
LOCALE_OVERRIDE: "zh_CN.UTF-8"
27+
# pandas does not use the language (zh_CN), but should support diferent encodings (utf8)
28+
# we should test with encodings different than utf8, but doesn't seem like Ubuntu supports any
29+
LANG: "zh_CN.utf8"
30+
LC_ALL: "zh_CN.utf8"
2731
EXTRA_APT: "language-pack-zh-hans"
2832

2933
py36_locale:
3034
ENV_FILE: ci/deps/azure-36-locale.yaml
3135
CONDA_PY: "36"
3236
PATTERN: "not slow and not network"
33-
LOCALE_OVERRIDE: "it_IT.UTF-8"
37+
LANG: "it_IT.utf8"
38+
LC_ALL: "it_IT.utf8"
39+
EXTRA_APT: "language-pack-it"
3440

3541
py36_32bit:
3642
ENV_FILE: ci/deps/azure-36-32bit.yaml
@@ -42,7 +48,9 @@ jobs:
4248
ENV_FILE: ci/deps/azure-37-locale.yaml
4349
CONDA_PY: "37"
4450
PATTERN: "not slow and not network"
45-
LOCALE_OVERRIDE: "zh_CN.UTF-8"
51+
LANG: "zh_CN.utf8"
52+
LC_ALL: "zh_CN.utf8"
53+
EXTRA_APT: "language-pack-zh-hans"
4654

4755
py37_np_dev:
4856
ENV_FILE: ci/deps/azure-37-numpydev.yaml
@@ -54,10 +62,16 @@ jobs:
5462

5563
steps:
5664
- script: |
57-
if [ "$(uname)" == "Linux" ]; then sudo apt-get install -y libc6-dev-i386 $EXTRA_APT; fi
58-
echo '##vso[task.prependpath]$(HOME)/miniconda3/bin'
59-
echo "Creating Environment"
60-
ci/setup_env.sh
65+
if [ "$(uname)" == "Linux" ]; then
66+
sudo apt-get update
67+
sudo apt-get install -y libc6-dev-i386 $EXTRA_APT
68+
fi
69+
displayName: 'Install extra packages'
70+
71+
- script: echo '##vso[task.prependpath]$(HOME)/miniconda3/bin'
72+
displayName: 'Set conda path'
73+
74+
- script: ci/setup_env.sh
6175
displayName: 'Setup environment and build pandas'
6276

6377
- script: |

ci/run_tests.sh

-11
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,6 @@
55
# https://github.com/pytest-dev/pytest/issues/1075
66
export PYTHONHASHSEED=$(python -c 'import random; print(random.randint(1, 4294967295))')
77

8-
if [ -n "$LOCALE_OVERRIDE" ]; then
9-
export LC_ALL="$LOCALE_OVERRIDE"
10-
export LANG="$LOCALE_OVERRIDE"
11-
PANDAS_LOCALE=`python -c 'import pandas; pandas.get_option("display.encoding")'`
12-
if [[ "$LOCALE_OVERRIDE" != "$PANDAS_LOCALE" ]]; then
13-
echo "pandas could not detect the locale. System locale: $LOCALE_OVERRIDE, pandas detected: $PANDAS_LOCALE"
14-
# TODO Not really aborting the tests until https://github.com/pandas-dev/pandas/issues/23923 is fixed
15-
# exit 1
16-
fi
17-
fi
18-
198
if [[ "not network" == *"$PATTERN"* ]]; then
209
export http_proxy=http://1.2.3.4 https_proxy=http://1.2.3.4;
2110
fi

ci/setup_env.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#!/bin/bash -e
22

33
# edit the locale file if needed
4-
if [ -n "$LOCALE_OVERRIDE" ]; then
4+
if [[ "$(uname)" == "Linux" && -n "$LC_ALL" ]]; then
55
echo "Adding locale to the first line of pandas/__init__.py"
66
rm -f pandas/__init__.pyc
7-
SEDC="3iimport locale\nlocale.setlocale(locale.LC_ALL, '$LOCALE_OVERRIDE')\n"
7+
SEDC="3iimport locale\nlocale.setlocale(locale.LC_ALL, '$LC_ALL')\n"
88
sed -i "$SEDC" pandas/__init__.py
9+
910
echo "[head -4 pandas/__init__.py]"
1011
head -4 pandas/__init__.py
1112
echo
12-
sudo locale-gen "$LOCALE_OVERRIDE"
1313
fi
1414

1515
MINICONDA_DIR="$HOME/miniconda3"

pandas/tests/config/test_localization.py

+22-10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
from pandas.compat import is_platform_windows
1010

11+
import pandas as pd
12+
1113
_all_locales = get_locales() or []
1214
_current_locale = locale.getlocale()
1315

@@ -56,21 +58,21 @@ def test_get_locales_prefix():
5658

5759

5860
@_skip_if_only_one_locale
59-
def test_set_locale():
61+
@pytest.mark.parametrize(
62+
"lang,enc",
63+
[
64+
("it_CH", "UTF-8"),
65+
("en_US", "ascii"),
66+
("zh_CN", "GB2312"),
67+
("it_IT", "ISO-8859-1"),
68+
],
69+
)
70+
def test_set_locale(lang, enc):
6071
if all(x is None for x in _current_locale):
6172
# Not sure why, but on some Travis runs with pytest,
6273
# getlocale() returned (None, None).
6374
pytest.skip("Current locale is not set.")
6475

65-
locale_override = os.environ.get("LOCALE_OVERRIDE", None)
66-
67-
if locale_override is None:
68-
lang, enc = "it_CH", "UTF-8"
69-
elif locale_override == "C":
70-
lang, enc = "en_US", "ascii"
71-
else:
72-
lang, enc = locale_override.split(".")
73-
7476
enc = codecs.lookup(enc).name
7577
new_locale = lang, enc
7678

@@ -91,3 +93,13 @@ def test_set_locale():
9193
# Once we exit the "with" statement, locale should be back to what it was.
9294
current_locale = locale.getlocale()
9395
assert current_locale == _current_locale
96+
97+
98+
def test_encoding_detected():
99+
system_locale = os.environ.get("LC_ALL")
100+
system_encoding = system_locale.split(".")[-1] if system_locale else "utf-8"
101+
102+
assert (
103+
codecs.lookup(pd.options.display.encoding).name
104+
== codecs.lookup(system_encoding).name
105+
)

pandas/tests/io/test_common.py

+17-9
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,15 @@ def test_read_non_existant(self, reader, module, error_class, fn_ext):
146146
msg3 = "Expected object or value"
147147
msg4 = "path_or_buf needs to be a string file path or file-like"
148148
msg5 = (
149-
r"\[Errno 2\] File .+does_not_exist\.{} does not exist:"
150-
r" '.+does_not_exist\.{}'"
151-
).format(fn_ext, fn_ext)
149+
fr"\[Errno 2\] File .+does_not_exist\.{fn_ext} does not exist:"
150+
fr" '.+does_not_exist\.{fn_ext}'"
151+
)
152+
msg6 = fr"\[Errno 2\] 没有那个文件或目录: '.+does_not_exist\.{fn_ext}'"
153+
msg7 = (
154+
fr"\[Errno 2\] File o directory non esistente: '.+does_not_exist\.{fn_ext}'"
155+
)
152156
with pytest.raises(
153-
error_class, match=r"({}|{}|{}|{}|{})".format(msg1, msg2, msg3, msg4, msg5)
157+
error_class, match=fr"({msg1}|{msg2}|{msg3}|{msg4}|{msg5}|{msg6}|{msg7})"
154158
):
155159
reader(path)
156160

@@ -177,17 +181,21 @@ def test_read_expands_user_home_dir(
177181
path = os.path.join("~", "does_not_exist." + fn_ext)
178182
monkeypatch.setattr(icom, "_expand_user", lambda x: os.path.join("foo", x))
179183

180-
msg1 = r"File (b')?.+does_not_exist\.{}'? does not exist".format(fn_ext)
184+
msg1 = fr"File (b')?.+does_not_exist\.{fn_ext}'? does not exist"
181185
msg2 = fr"\[Errno 2\] No such file or directory: '.+does_not_exist\.{fn_ext}'"
182186
msg3 = "Unexpected character found when decoding 'false'"
183187
msg4 = "path_or_buf needs to be a string file path or file-like"
184188
msg5 = (
185-
r"\[Errno 2\] File .+does_not_exist\.{} does not exist:"
186-
r" '.+does_not_exist\.{}'"
187-
).format(fn_ext, fn_ext)
189+
fr"\[Errno 2\] File .+does_not_exist\.{fn_ext} does not exist:"
190+
fr" '.+does_not_exist\.{fn_ext}'"
191+
)
192+
msg6 = fr"\[Errno 2\] 没有那个文件或目录: '.+does_not_exist\.{fn_ext}'"
193+
msg7 = (
194+
fr"\[Errno 2\] File o directory non esistente: '.+does_not_exist\.{fn_ext}'"
195+
)
188196

189197
with pytest.raises(
190-
error_class, match=r"({}|{}|{}|{}|{})".format(msg1, msg2, msg3, msg4, msg5)
198+
error_class, match=fr"({msg1}|{msg2}|{msg3}|{msg4}|{msg5}|{msg6}|{msg7})"
191199
):
192200
reader(path)
193201

0 commit comments

Comments
 (0)