|
1 | 1 | # Tests the attempted automatic coercion of the C locale to a UTF-8 locale
|
2 | 2 |
|
3 | 3 | import unittest
|
| 4 | +import locale |
4 | 5 | import os
|
5 | 6 | import sys
|
6 | 7 | import sysconfig
|
|
32 | 33 |
|
33 | 34 | # In order to get the warning messages to match up as expected, the candidate
|
34 | 35 | # order here must much the target locale order in Python/pylifecycle.c
|
35 |
| -_C_UTF8_LOCALES = ("C.UTF-8", "C.utf8") #, "UTF-8") |
36 |
| - |
37 |
| -# XXX (ncoghlan): Using UTF-8 as a target locale is currently disabled due to |
38 |
| -# problems encountered on *BSD systems with those test cases |
39 |
| -# For additional details see: |
40 |
| -# nl_langinfo CODESET error: https://bugs.python.org/issue30647 |
41 |
| -# locale handling differences: https://bugs.python.org/issue30672 |
| 36 | +_C_UTF8_LOCALES = ("C.UTF-8", "C.utf8", "UTF-8") |
42 | 37 |
|
43 | 38 | # There's no reliable cross-platform way of checking locale alias
|
44 | 39 | # lists, so the only way of knowing which of these locales will work
|
45 | 40 | # is to try them with locale.setlocale(). We do that in a subprocess
|
46 | 41 | # to avoid altering the locale of the test runner.
|
| 42 | +# |
| 43 | +# If the relevant locale module attributes exist, and we're not on a platform |
| 44 | +# where we expect it to always succeed, we also check that |
| 45 | +# `locale.nl_langinfo(locale.CODESET)` works, as if it fails, the interpreter |
| 46 | +# will skip locale coercion for that particular target locale |
| 47 | +_check_nl_langinfo_CODESET = bool( |
| 48 | + sys.platform not in ("darwin", "linux") and |
| 49 | + hasattr(locale, "nl_langinfo") and |
| 50 | + hasattr(locale, "CODESET") |
| 51 | +) |
| 52 | + |
47 | 53 | def _set_locale_in_subprocess(locale_name):
|
48 | 54 | cmd_fmt = "import locale; print(locale.setlocale(locale.LC_CTYPE, '{}'))"
|
| 55 | + if _check_nl_langinfo_CODESET: |
| 56 | + # If there's no valid CODESET, we expect coercion to be skipped |
| 57 | + cmd_fmt += "; import sys; sys.exit(not locale.nl_langinfo(locale.CODESET))" |
49 | 58 | cmd = cmd_fmt.format(locale_name)
|
50 | 59 | result, py_cmd = run_python_until_end("-c", cmd, __isolated=True)
|
51 | 60 | return result.rc == 0
|
52 | 61 |
|
| 62 | + |
| 63 | + |
53 | 64 | _fields = "fsencoding stdin_info stdout_info stderr_info lang lc_ctype lc_all"
|
54 | 65 | _EncodingDetails = namedtuple("EncodingDetails", _fields)
|
55 | 66 |
|
|
0 commit comments