Skip to content
Closed
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
23 changes: 23 additions & 0 deletions Lib/test/test_c_locale_coercion.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,13 @@ def get_child_details(cls, env_vars):
AVAILABLE_TARGETS = None
CLI_COERCION_TARGET = None
CLI_COERCION_WARNING = None
DEFAULT_LC_CTYPE = None

def setUpModule():
global AVAILABLE_TARGETS
global CLI_COERCION_TARGET
global CLI_COERCION_WARNING
global DEFAULT_LC_CTYPE

if AVAILABLE_TARGETS is not None:
# initialization already done
Expand All @@ -181,6 +183,22 @@ def setUpModule():
CLI_COERCION_TARGET = AVAILABLE_TARGETS[0]
CLI_COERCION_WARNING = CLI_COERCION_WARNING_FMT.format(CLI_COERCION_TARGET)

# Determine the platform's default LC_CTYPE from the default locale.
# POSIX provides that there *exists* a default locale to provide when
# no environment variables are set, or are set to empty strings. However,
# it does not specify that this default *must* be the "POSIX" locale, or
# how the platform selects the default. In most cases it will be set to
# "C"/"POSIX" but this is not necessarily the case.
result, py_cmd = run_python_until_end(
"-c", "import locale; print(locale.setlocale(locale.LC_CTYPE))",
__isolated=True,
LANG="", LC_ALL="", LC_CTYPE="", PYTHONCOERCECLOCALE="0"
)
if not result.rc == 0:
result.fail(py_cmd)

DEFAULT_LC_CTYPE = result.out.decode("ascii").strip().upper()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Did you try setting C_LOCALE_STREAM_ENCODING to "utf-8" instead ?


class _LocaleHandlingTestCase(unittest.TestCase):
# Base class to check expected locale handling behaviour
Expand Down Expand Up @@ -301,6 +319,11 @@ def _check_c_locale_coercion(self,
# See https://bugs.python.org/issue30672 for discussion
if locale_to_set == "POSIX":
continue
elif locale_to_set == "" and DEFAULT_LC_CTYPE == "C.UTF-8":
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The condition is too restrictive:

  • When locale_to_set is "" and a locale envt variable exists in extra_vars with a non empty string (for example in test_LC_ALL_set_to_C), the subtest may be run.
  • It seems that when C_LOCALE_STREAM_ENCODING is set to "utf-8" and coerce_c_localecoerce_c_locale is not "warn" then the test should pass.

# The default locale is already C.UTF-8 so the subsequent tests
# don't behave as expected (they assume the default LC_CTYPE will
# be "C")
continue
with self.subTest(env_var=env_var,
nominal_locale=locale_to_set,
PYTHONCOERCECLOCALE=coerce_c_locale):
Expand Down